thinkphp3.2 Verification code generation and click Refresh Verification Code

Source: Internet
Author: User
Tags vars

Background: Said before the project, lazy login module did not add verification code, think of or add, find the next TP document, found that there is integration, special record.

First, instantiate the class that generated the verification code (this method is easy to access in Indexcontroller)

[PHP] View plaincopy
  1. /**
  2.  * 
  3. * Verification Code generation
  4.  */   
  5. Public function verify_c () {
  6. $Verify  =  New  \think\verify ();
  7. $Verify  ->fontsize = 18;
  8. $Verify  ->length = 4;
  9. $Verify  ->usenoise = false;
  10. $Verify ->codeset = ' 0123456789 '  ;
  11. $Verify  ->imagew = 130;
  12. $Verify  ->imageh = 50;
  13. //$Verify->expire = +;   
  14. $Verify  ->entry ();
  15. }
second, the foreground needs to generate a verification code image SRC attribute point to [HTML] View plaincopy
  1. < P   class =   id = " Captcha-container " >   
  2.   < input   name = "verify"   width = "50%"   height = ""   class = "Captcha-text"   placeholder = "verification Code"   type = "text" >                    
  3.   < img   width = "30%"   class = "left15"   height = ""   alt = "verification Code"   src =   title = "click to refresh" >   
  4. </ P >   
three, after the completion of the above, the page initialization verification code can appear, the following to write is to click on the verification code after the image, refresh the new verification code image (through jquery modified the SRC attribute of the image to complete, the request processing function, just add a random number after the request, A request to distinguish the previous picture) [JavaScript] View plaincopy
  1. //Verification code generation   
  2. var captcha_img = $ (' #captcha-container '). Find (' img ')
  3. var  verifyimg = captcha_img.attr ("src");
  4. Captcha_img.attr (' title', ' Click to Refresh ');
  5. Captcha_img.click (function() {
  6. if (Verifyimg.indexof ('? ' ) ) {>0) {
  7. $ (this). attr ("src", verifyimg+' &random= '+math.random ());
  8. }Else{
  9. $ (this). attr ("src", Verifyimg.replace (/\). *$/,') +'? '  +math.random ());
  10. }
  11. });
Verification code input is correct

A. Add a global function to the function.php in the common directory

[PHP] View plaincopy
  1. /**
  2. * Verification Code Check
  3.  */   
  4. function check_verify ($code, $id = "") {
  5. $verify  =  New  \think\verify ();
  6. return   $verify->check ($code, $id);
  7. }
B. Add a check code to the handler corresponding to the form submitted by the Controller [PHP] View plaincopy
  1. //Check the verification code   
  2. $verify  = I (' param.verify ',');
  3. if (!check_verify ($verify)) {
  4. $this ->error ("Pro, the verification code is wrong!")  ",$this->site_url,9);
  5. }
The use of this tp3.2 verification code is possible.

Add: When I write, I put four of the B-Step into an AJAX validation, returning a test result. Then based on the return of the results to determine whether to submit the form, but after the verification code through the first check, the second will not be able to, at present, do not want to understand why.

thinkphp3.2 Verification code generation and click Refresh Verification Code

Related Article

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.