This article is mainly for you to introduce the thinkphp implementation of the verification code to generate and verify the function, with a certain reference value, interested in small partners can refer to
Use thinkphp to implement generate/Verify verification code function, for your reference, the specific content as follows
First understand some of the functions in the parent class Verity.class.php (thinkphp/library/think/verity.class.php)
1:check () Verify that the verification code is correct
2:entry () output The verification code and save the value of the verification code in the session
3:authcode () Encryption Verification code (this method is called in the Check () method)
The controller generates the CAPTCHA code:
Verification Code Function verifyimg () { //CAPTCHA configuration $cfg = Array ( ' Imageh ' + 30,//CAPTCHA image height ' imagew ' = + 100,//verification code Image width ' fontSize ' + 15,//Captcha font size (px) ' length ' = 4,//Verify code number ' Fontttf ' + ' 4.ttf ',//Captcha font, not set random get c8/>); Instantiate the Verify class to automatically call the constructor and pass the $cfg information to $config $verify = new Verify ($CFG); $verify-entry (); Output Verification Code}
Verification code displayed at front desk
Copy the Code code as follows:
<!--Group/Controller/Method--/verifyimg" onclick = "this.src=" <{$ smarty.const.__controller__}>/verifyimg/' +math.random () "alt=" Verification Code "/>
Controller Check Verification code (take login as an example)
Login function Login () { //Two logic: show, collect if (!empty ($_post)) { $verify = new Verify (); Receive verification code $captcha = $_post[' Captcha ']; Check () method verifies that the code if ($verify->check ($captcha)) { echo "is correct"; } else{ echo "error"; } } Call the View view $this->display ();}
The above is the whole content of this article, I hope that everyone's study has helped.