Thinkphp3.2 examples of computing verification codes and thinkphp3.2 examples
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:
Public function entry_add ($ id = '') {$ this-> length = '3'; // image width (px) $ this-> imageW | $ this-> imageW = $ this-> length * $ this-> fontSize * 1.5 + $ this-> length * $ this-> fontSize/2; // Image Height (px) $ this-> imageH | $ this-> imageH = $ this-> fontSize * 2.5; // create an image $ this-> imageW x $ this-> imageH $ this-> _ image = imagecreate ($ this-> imageW, $ this-> imageH ); // set the background imagecolorallocate ($ this-> _ image, $ this-> bg [0], $ this-> Bg [1], $ this-> bg [2]); // random color of the font of the Verification Code $ this-> _ color = imagecolorallocate ($ this-> _ image, mt_rand (1,150), mt_rand (1,150), mt_rand (1,150); // The Verification Code uses a random font $ ttfPath = dirname (_ FILE __). '/Verify /'. ($ this-> useZh? 'Zhttfs': 'ttfs '). '/'; if (empty ($ this-> fontttf) {$ dir = dir ($ ttfPath); $ ttfs = array (); while (false! ==( $ File = $ dir-> read () {if ($ file [0]! = '. '& Substr ($ file,-4) = '. ttf') {$ ttfs [] = $ file ;}$ dir-> close (); $ this-> fontttf = $ ttfs [array_rand ($ ttfs)];} $ this-> fontttf = $ ttfPath. $ this-> fontttf; if ($ this-> useImgBg) {$ this-> _ background ();} if ($ this-> useNoise) {// $ this-> _ writeNoise ();} if ($ this-> useCurve) {// $ this-> _ writeCurve ();} // print the verification code $ code = array (); // The Verification code $ symbol = array ('+', '-'); $ codeNX = 0; // The left margin of the nth character of the Verification Code $ now_symbol = $ symbol [rand (0, 1)]; for ($ I = 0; $ I <$ this-> length; $ I ++) {if ($ I = 1) {$ code [$ I] = $ now_symbol; $ codeNX + = mt_rand ($ this-> fontSize * 1.2, $ this-> fontSize * 1.6); imagettftext ($ this-> _ image, $ this-> fontSize, 0, $ codeNX, $ this-> fontSize * 1.6, $ this-> _ color, $ ttfPath. '2. ttf', $ code [$ I]);} else {$ code [$ I] = $ this-> codeSet [mt_rand (0, strlen ($ this-> codeSet) -1)]; $ codeNX + = mt_rand ($ this-> fontSize * 1.2, $ this-> fontSize * 1.6); imagettftext ($ this-> _ image, $ this-> fontSize, mt_rand (-40, 40), $ codeNX, $ this-> fontSize * 1.6, $ this-> _ color, $ this-> fontttf, $ code [$ I]) ;}}// Save the verification code $ key =$ this-> authcode ($ this-> seKey); $ str = implode ('', $ code); eval ("\ $ re = $ str;"); $ code = $ this-> authcode ($ re); $ secode = array (); $ secode ['verify _ Code'] = $ code; // Save the verification code to session $ secode ['verify _ time'] = NOW_TIME; // verification code creation time session ($ key. $ id, $ secode); header ('cache-Control: private, max-age = 0, no-store, no-Cache, must-revalidate '); header ('cache-Control: post-check = 0, pre-check = 0', false); header ('pragma: no-cache '); header ("content-type: image/png"); // output image imagepng ($ this-> _ image); imagedestroy ($ this-> _ image );}
Public function check_add ($ code, $ id = '') {$ key = $ this-> authcode ($ this-> seKey ). $ id; // The Verification code cannot be blank $ secode = session ($ key); if ($ code = false | empty ($ secode) {return false ;} // whether the verification code is a number if (! Is_numeric ($ code) {return false;} // session expiration if (NOW_TIME-$ secode ['verify _ time']> $ this-> expire) {session ($ key, null); return false;} if ($ this-> authcode ($ code) ==$ secode ['verify _ Code']) {$ this-> reset & session ($ key, null); return true;} return false ;}
Generation Method:
Public function verify(){ import('ORG.Util.Verify'); $Verify = new Verify(); $Verify->useNoise = true; $Verify->codeSet = '0123456789'; $Verify->useCurve = false; $Verify->entry_add(); }
Verification Method:
If (! Check_verify ($ verify, '', 'add') {$ this-> error ('verification code error! '); Return ;}
Public method called:
// Check whether the entered verification code is correct. $ code is the user-entered verification code string function check_verify ($ code, $ id = '', $ type = '') {import ('org. util. verify '); $ verify = new Verify (); if ($ type = 'add') {return $ verify-> check_add ($ code, $ id );} else {return $ verify-> check ($ code, $ id );}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.