thinkphp 3.2.3-----Verification Code

Source: Internet
Author: User

Show Verification Code

First create a public controller under Home/controller Publiccontroller

1 <?php 2 namespace Home\controller; 3  4 Use Think\controller, 5 use think\verify; 6  7 class Publiccontroller extends Controller 8 {9     */* Generate Verification code */11 Public     function Verify ()     {         $config = [fontSize ' +             19,//captcha font size             ' length ' = 4 ,//Verification code number             : ' imageh '         = 3417];18         $Verify = new Verify ($config);         $Verify->entry (); 20     }21     /* Verification Code Check */23 public     function check_verify ($code, $id = ")     {         $verify = new \think\v Erify ();         $res = $verify->check ($code, $id);         $this->ajaxreturn ($res, ' json ');     }29}
The Verify function is used to generate a verification code, which is used to configure the properties of the display verification code. What are the configurable items for this property, you can view the thinkphp/library/think/verify.class.php file, which is not mentioned here.
The Check_verify function is used to verify the correctness of the verification code. The template post user fills in the verification code to the function, and returns $RES==TRUE validation by false to verify the failure.
Foreground template Page Build index.html
1 <div class= "" >2 <label for= "j_verify" class= "T" > Captcha:</label> <input id= "j_verify" 3 name= "j_ Verify "type=" text "class=" Form-control x164 in ">4 6 </div>

Use the thinkphp U method to form a picture that generates a verification code.

The verification code is displayed as normal. Click Refresh Verification Code

From the above to generate a link to the verification code, we can see that the domain name/public/verify can generate a verification code. thinkphp Verification Code Generation mechanism is, if we need to generate a new verification code, after the link to add a variable value.

We can consider the form URL that implements the value of the public/verify/variable.

1 $ ("#verify_img"). Click (function () {2    var verifyurl = "Public/verify"; 3    var time = new Date (). GetTime (); 4    $ ("#verify_img"). attr ({5       "src": Verifyurl + "/" + Time6    }); 7});

Use JS to get the current timestamp after adding to the URL.

Here we can realize the click Refresh function of verification code. concrete manifestation of the form, self-free ha.

Asynchronous checksum verification Code

We must have seen some Web sites, when we enter the verification code, the input process after the text box has been displayed error, until we enter the fight will first prompt the correct form of the code. The following code can be implemented:

1 $ ("#j_verify"). KeyUp (function () {2     $.post ("Public/check_verify", {3         code: $ ("#j_verify"). Val () 4         }, function (data) {5         if (data = = True) {6             //Verification code input correct 7         } else {8             //CAPTCHA input Error 9         }10     }); 11});

Use Onekeyup. The principle does not need to speak more!

So that we can implement the user name password before submitting a verification code check, after the form is submitted and then check again, improve the user experience!

It's not over yet:

When we take the above form to achieve the verification code two check, the first time the asynchronous verification will be successful, but the submission of the form is prompted to verify the wrong code! In fact, the reason is still on the thinkphp Verify.class.php:

The following is the initial config configuration for this class:

protected $config =    Array (        ' sekey ' = '  thinkphp.cn ',   //Authenticode encryption key        ' codeset '   =  ' 2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY ',             //captcha Character set        ' expire ' +  1800,            //Verification code expiry time (s)        ' Usezh '     +  false,           //Use Chinese captcha         ' zhset '     =  ' ... This is not pasted here, too much! ',//              Chinese captcha string        ' useimgbg '  +  false,           //Use background image         ' fontSize '  =  25,              //Captcha font size (px)        ' Usecurve '  and False,//            whether to draw confusion curve        ' usenoise '  =  False,//            whether to add the miscellaneous dot            ' Imageh ' +  0,               //captcha Image height        ' imagew '    = +  0,               // Captcha picture width        ' length '    =  5,               //Verify code number ' Fontttf ' + ', '/              /captcha font, not set random get        ' bg '        = =  Array (243, 251, 254),  //Background color        ' reset '     =  true,           //Verify if Reset is successful        );

Please note that the last property, reset, indicates whether the verification code is reset after successful validation. I believe we all understand, we do the first check pass, in fact, the verification code has been invalidated, but because we are taking an asynchronous check, the page is not refreshed, resulting in the second application of the check and the system has been generated is not the same. So if you like the two-check style, consider configuring reset to False.

OK, the verification code problem based on Thinkphp3.2.3 is summed up here today. Just about to say a bit about the implementation method, the specific content also please refer to the official TP manual. If there is any mistake, you are welcome to point out.

thinkphp 3.2.3-----Verification Code

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.