Then introduce the use of thinkphp3.2 verification code before, for everyone to introduce the thinkphp verification code, the specific content as follows
The thinkphp has built-in verification code support, which can be used directly. To use a verification code, you need to import the ORG in the Extended class library. Util.image class libraries and ORG. Util.string class Library.
Verification Code Method
We add a verify method to the module class to display the verification code, the simplest example:
Public Function Verify () { //import the Image Class library import ("ORG. Util.image "); Image::buildimageverify ();}
The import method is the thinkphp built-in class library and file import method, and the file imported is the lib/org/util/image.class.php file in the thinkphp system directory. If you have copied the Image class library under the current project, such as lib/org, you can:
Import ("@. Util.image ");
The import method is the thinkphp built-in class library and file import method, and the file imported is the lib/org/util/image.class.php file in the thinkphp system directory.
Access Verification Code
The Captcha method can be accessed directly in the browser to determine if the verification code is displayed correctly:
Http://127.0.0.1/index.php/Public/verify
If everything is OK, the display verification code is as follows:
Use a verification code in a form
Using the captcha in the form page is called with the HTML IMG tag:
<input type= "Text" name= "verify" >
The SRC attribute value is the authentication code method access address, depending on the actual situation and different.
Verification Code Refresh
When you click on the Captcha picture, the JavaScript changeverify () function is triggered to re-read the verification code, which allows the code to be refreshed. The function is referenced as follows:
<script language= "JavaScript" >function changeverify () {var timenow = new Date (). GetTime (); document.getElementById (' verifyimg '). src= '-article/verify/' +timenow;} </script>
Verification Code Verification
When the verification code verify is called, Buildimageverify will record the MD5 information of this verification code. In the form validation operation, check that the captcha is correct in the following ways:
if ($_session[' verify '! = MD5 ($_post[' verify '])) { $this->error (' Captcha is wrong! ');}
where $_session[' verify ' Verify name is called the Buildimageverify method default SESSION registration name, see the buildimageverify syntax.
The above example demonstrates the simplest way to use the thinkphp verification code. The above example verification code is 4 digits, if you want to use more style verification code and Chinese verification Code, see the remainder of this section: "Thinkphp use different styles and Chinese authentication code."
The verification code does not show the reason
The verification code cannot be displayed as follows, possibly for the following reasons:
1, whether PHP has installed the GD library support.
2, if there is any output before the output (especially the UTF8 BOM header information output).
3. Whether the Image class library is imported correctly.
4. If it is a form page, see if the Captcha display method is called correctly.
The following is to introduce thinkphp3.2 verification code generation and click Refresh Verification Code implementation method, the specific content is as follows
First, instantiate the class that generated the verification code (this method is easy to access in Indexcontroller)
/** * * Verification code generated */public function Verify_c () { $Verify = new \think\verify (); $Verify->fontsize =; $Verify->length = 4; $Verify->usenoise = false; $Verify->codeset = ' 0123456789 '; $Verify->imagew =; $Verify->imageh =; $Verify->expire = +; $Verify->entry ();}
Second, the foreground needs to generate a verification code image SRC attribute point to
<p class= "Top15 captcha" id= "Captcha-container" > <input name= "verify" width= "50%" height= "class=" Captcha-text "placeholder=" Verification Code "type=" text "> </p>
Three, after the completion of the above, the page initialization of the 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 image of the SRC attribute to complete, the request processing function, just add a random number after the request, to distinguish the previous picture request)
The verification code generates VAR captcha_img = $ (' #captcha-container '). FIND (' img ') var verifyimg = captcha_img.attr ("src"); captcha_img.attr (' title ', ' click Refresh '); Captcha_img.click (function () { if (verifyimg.indexof ('? ') >0) { $ (this). attr ("src", verifyimg+ ' &random= ' +math.random ()); } else{ $ (this). attr ("src", Verifyimg.replace (/\?). *$/,'')+'?' +math.random ());} );
Verification code input is correct
A. Add a global function to the function.php in the common directory
/** * Verification Code Check */function check_verify ($code, $id = "") { $verify = new \think\verify (); Return $verify->check ($code, $id);}
B. Add a check code to the handler corresponding to the form submitted by the Controller
Check the verification code $verify = I (' param.verify ', '), if (!check_verify ($verify)) { $this->error ("Pro, the verification code is wrong! ", $this->site_url,9);}
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.
This is the whole content of this article, the article finally has a small question, I hope you can come up with a solution, but also hope that this article on everyone's learning to help.