Thinkphp method for generating and verifying the verification code function

Source: Internet
Author: User
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.


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.