"One" Verification code
Verification code Full Name: CAPTCHA (fully automatic identification machine and human Turing Test), simple understanding is to distinguish whether the current operation is performed by a person or machine
Common verification code in 3 kinds: page form, SMS Verification code (email verification can be categorized into SMS verification code, but the carrier is different. A text message one mail), Voice Verification code (click to call the phone, machine read two times verification code).
Note: Now the popular brush face is the verification form, not the verification code
(1) Image Verification code
GD Library is a php processing graphics extension Library, GD library provides a series of APIs for processing pictures, using the GD library can process pictures, or create pictures, you can also add watermarks to the picture.
GD operation Flow: ① canvas; ② draw interference line, interference point, generate verification code random number, ③ saved to session; ④ output image
In the thinkphp system encapsulates the verification code class: Verify.class.php, file location thinkphp/library/think/verify.class.php
//The verification code stored in the session is not stored in plaintext, but is MD5 in encrypted format. MD5 encryption, not between encryption, but first connect Sekey, intercept how many of them again MD5 encryption: MD5 (123,sekey)' Sekey ' = ' thinkphp.cn ',//Verification Code Encryption key
' codeset ' = ' 2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY ',Verification Code Character Set
' expire ' = 1800,Verification code Expiration Time (s)
' Usezh ' = False,Use Chinese Verification Code
' Zhset ' = ' I will move to China when he will do ... '
' USEIMGBG ' + false,//Use background image, default is not used, because the own TP background image is a bit ugly ...
' FontSize ' + 25,//Captcha font size (px), the default 25px is a Little big oh ...
' Usecurve ' = true,//whether to draw confusion curve
' Usenoise ' = true,//whether to add a miscellaneous point
' Imageh ' + 0,//CAPTCHA Image Height
' Imagew ' + 0,//captcha image width
' Length ' + 5,//Verification code number, default 5, general image is 4, mobile phone verification Code 6
' Fontttf ' = ' + ',//Captcha font, not set for random acquisition
' BG ' = = Array (243, 251, 254),//Background color
' Reset ' = true,//verify if the Reset is successful
① Encryption method
Specific encryption process at the bottom of the file location
/* cryptographic captcha */ private function authcode ( $str $key = substr (md5 ( $this ->sekey), 5, 8); $str = substr (md5 ( $str ), 8, 10 return md5 ( $key . $str
This two-time encryption form is also called salt encryption/feed encryption, which may later be used in the data table.
② Verification Code Character set
By carefully observing the CAPTCHA character set, you can find a lot less characters, such as 1,9l,g. Because 0 and o,g and 9 are very similar, so in order to avoid frequent refresh of the verification code, reduce the burden on the server, so the confusing characters are removed
③ Verification Code Expiration Time
Because the verification code is stored in the session, it will be invalidated if no input is validated within the specified time.
④ about the fonts and backgrounds that are set inside the sibling folder verify
⑤ Method Introduction
1. Construction method: An array can be passed during instantiation to merge with its Member property config to generate a new configuration
Public function __construct ($config=array()) { $this->config = array_merge($this$config); }
2. Check method: Verify the verification code, pass the parameters (user input verification code)
/** * Verify that the verification code is correct * @access public * @param string $code User Verification code * @param string $id authenticode identification * @retu RN BOOL User Verification code is correct*/ Public functionCheck$code,$id= ' ') { $key=$this->authcode ($this->sekey).$id; //The verification code cannot be empty $secode= Session ($key); if(Empty($code) ||Empty($secode)) { return false; } //Session Expires if(Now_time-$secode[' Verify_time '] >$this-expire) {Session ($key,NULL); return false; } if($this->authcode (Strtoupper($code)) ==$secode[' Verify_code ']) { $this-Reset&& session ($key,NULL); return true; } return false; }
3. Entry method: Output the CAPTCHA image and save it to the session. Output format PNG via header header output
Header (' Cache-control:private, max-age=0, No-store, No-cache, Must-revalidate '); Header false ); Header (' Pragma:no-cache '); Header ("Content-type:image/png");
4. In addition to the above methods (configuration, check, output), other methods (such as encryption verification code, drawing background map, draw the dots, etc.) are private methods, not open
Video Learning Transcript---thinkphp---TP function class