This article mainly introduces the Thinkphp3.2 practical text Verification code example, the content is very good, and now share to everyone, but also for everyone to do a reference.
Do not feel that the ordinary verification code has not been able to meet, the next introduction of how to change the current TP code to calculate the verification code:
First found: thinkphp\library\think\verify.class.php
Add the following code to it:
Public Function Entry_add ($id = ") {$this->length= ' 3 '; Picture width (px) $this->imagew | | $this->imagew = $this->length* $this->fontsize*1.5 + $this->length* $this->fontsize/2; Picture 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 Background imagecolorallocate ($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]); CAPTCHA font random color $this->_color = imagecolorallocate ($this->_image, Mt_rand (1,150), Mt_rand (1,150), Mt_rand (1,150)); The captcha 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) {//Plot dot $this->_writenoise (); } if ($this->usecurve) {//Draw interference line $this->_writecurve (); }//Draw verification Code $code = Array (); Verification Code $symbol =array (' + ', '-'); $codeNX = 0; The left margin of the nth character of the captcha $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, Max), $codeNX, $this->fontsize*1.6, $this->_color, $this->fontttf, $ code[$i]); }}//Save 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 check code to the 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 empty $secode = Session ($key); if ($code ===false | | empty ($secode)) { return false; } Whether the captcha is a numeric if (!is_numeric ($code)) { return false; } Session expires 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; }
Build 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 (' Captcha is wrong! '); return; }
Public methods to invoke:
Verify the input verification code is correct, $code for the user input 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 the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!