Computing verification code and Thinkphp3.2 Verification Code

Source: Internet
Author: User

Computing verification code and Thinkphp3.2 Verification Code

Do you think that common verification codes cannot be met? Next we will introduce how to change the existing tp verification codes to computation verification codes:

First, find ThinkPHP \ Library \ Think \ Verify. class. php.

Add the following code:

1 public function entry_add ($ id = '') {2 $ this-> length = '3'; 3 // picture width (px) 4 $ this-> imageW | $ this-> imageW =$ this-> length * $ this-> fontSize * 1.5 + $ this-> length * $ this-> fontSize/ 2; 5 // picture height (px) 6 $ this-> imageH | $ this-> imageH = $ this-> fontSize * 2.5; 7 // create an image $ this-> imageW x $ this-> imageH image 8 $ this-> _ image = imagecreate ($ this-> imageW, $ this-> imageH); 9 // Set background 10 imagecolorallocate ($ this-> _ image, $ This-> bg [0], $ this-> bg [1], $ this-> bg [2]); 11 12 // random font color of the Verification Code 13 $ this-> _ color = imagecolorallocate ($ this-> _ image, mt_rand (1,150), mt_rand (1,150 ), mt_rand (1,150); 14 // The Verification Code uses a random font of 15 $ ttfPath = dirname (_ FILE __). '/Verify /'. ($ this-> useZh? 'Zhttfs': 'ttfs '). '/'; 16 17 if (empty ($ this-> fontttf) {18 $ dir = dir ($ ttfPath); 19 $ ttfs = array (); 20 while (false! ==( $ File = $ dir-> read () {21 if ($ file [0]! = '. '& Substr ($ file,-4) = '. ttf') {22 $ ttfs [] = $ file; 23} 24} 25 $ dir-> close (); 26 $ this-> fontttf = $ ttfs [array_rand ($ ttfs)]; 27} 28 $ this-> fontttf = $ ttfPath. $ this-> fontttf; 29 30 if ($ this-> useImgBg) {31 $ this-> _ background (); 32} 33 34 if ($ this-> useNoise) {35 // 36 36 $ this-> _ writeNoise (); 37} 38 if ($ this-> useCurve) {39 // painted interference line 40 $ this-> _ writeCurve (); 41} 42 43 // painted Verification code 44 $ code = array (); // verification code 45 $ symbol = array ('+', '-'); 46 $ codeNX = 0; // The left margin of the N character of the verification code is 47 $ now_symbol = $ symbol [rand (0, 1)]; 48 for ($ I = 0; $ I <$ this-> length; $ I ++) {49 if ($ I = 1) {50 $ code [$ I] = $ now_symbol; 51 $ codeNX + = mt_rand ($ this-> fontSize * 1.2, $ this-> fontSize * 1.6); 52 imagettftext ($ this-> _ image, $ this-> fontSize, 0, $ codeNX, $ this-> fontSize * 1.6, $ this-> _ color, $ ttfPath. '2. ttf', $ code [$ I]); 53} 54 else {55 $ code [$ I] = $ this-> codeSet [mt_rand (0, strlen ($ this-> codeSet)-1)]; 56 $ codeNX + = mt_rand ($ this-> fontSize * 1.2, $ this-> fontSize * 1.6 ); 57 imagettftext ($ this-> _ image, $ this-> fontSize, mt_rand (-40, 40), $ codeNX, $ this-> fontSize * 1.6, $ this-> _ color, $ this-> fontttf, $ code [$ I]); 58} 59} 60 61 // Save the verification code 62 $ key = $ this-> authcode ($ this-> seKey); 63 $ str = implode ('', $ code ); 64 eval ("\ $ re = $ str;"); 65 $ code = $ this-> authcode ($ re); 66 $ secode = array (); 67 $ secode ['verify _ Code'] = $ code; // Save the verification code to session68 $ secode ['verify _ time'] = NOW_TIME; // verification code creation time 69 session ($ key. $ id, $ secode); 70 71 header ('cache-Control: private, max-age = 0, no-store, no-Cache, must-revalidate '); 72 header ('cache-Control: post-check = 0, pre-check = 0', false); 73 header ('pragma: no-cache '); 74 header ("content-type: image/png"); 75 76 // output image 77 imagepng ($ this-> _ image ); 78 imagedestroy ($ this-> _ image); 79}
1 public function check_add ($ code, $ id = '') {2 $ key = $ this-> authcode ($ this-> seKey ). $ id; 3 // The Verification code cannot be blank. 4 $ secode = session ($ key); 5 if ($ code = false | empty ($ secode )) {6 return false; 7} 8 // whether the verification code is a number 9 if (! Is_numeric ($ code) {10 return false; 11} 12 // session expiration 13 if (NOW_TIME-$ secode ['verify _ time']> $ this-> expire) {14 session ($ key, null); 15 return false; 16} 17 if ($ this-> authcode ($ code) = $ secode ['verify _ Code']) {18 $ this-> reset & session ($ key, null); 19 return true; 20} 21 return false; 22}

Generation Method:

1 Public function verify(){2         import('ORG.Util.Verify');3         $Verify = new Verify();4         $Verify->useNoise = true;5         $Verify->codeSet = '0123456789';6         $Verify->useCurve = false;7         $Verify->entry_add();8     }

Verification Method:

1 if (! Check_verify ($ verify, '', 'add') {2 $ this-> error ('verification code error! '); 3 return; 4}

Public method called:

1 // check whether the entered verification code is correct. $ code is the user-entered verification code string 2 function check_verify ($ code, $ id = '', $ type = '') {3 import ('org. util. verify '); 4 $ verify = new Verify (); 5 if ($ type = 'add') {6 return $ verify-> check_add ($ code, $ id ); 7} 8 else {9 return $ verify-> check ($ code, $ id); 10} 11}

Complete, you can make full use of your imagination to improve.

Question: Do you really need to click the verification code of the inverted text on zhihu? If you are interested, you can check it out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.