Thinkphp3.2 click to refresh to generate a verification code, thinkphp3.2 Verification Code
Before using the thinkphp3.2 verification code, we will introduce the ThinkPHP verification code in detail. The details are as follows:
ThinkPHP has built-in support for verification codes and can be used directly. To use the verification code, you must import the ORG. Util. Image class library and ORG. Util. String class library in the extended class library.
Verification Code Method
We add a verify Method to the module class to display the verification code. The simplest example is as follows:
Public function verify () {// import the Image class library import ("ORG. Util. Image"); Image: buildImageVerify ();}
The import method is the built-in class library and File import method of ThinkPHP. The file imported in the above example is the Lib/ORG/Util/Image. class. php file in the ThinkPHP system directory. If you have copied the Image class library to the current project, such as Lib/ORG, you can run the following command:
import("@.Util.Image");
The import method is the built-in class library and File import method of ThinkPHP. The file imported in the above example is the Lib/ORG/Util/Image. class. php file in the ThinkPHP system directory.
Access Verification Code
You can directly access the verification code method in the browser to determine whether the verification code can be properly displayed:
Http: // 127.0.0.1/index. php/Public/verify
If everything is normal, the verification code is displayed as follows:
Use verification code in the form
Use the verification code on the form page, which is called using the html img Tag:
<Input type = "text" name = "verify">
The src attribute value is the access address of the verification code method, which varies depending on the actual situation.
Refresh Verification Code
When you click the verification code image, the JavaScript changeVerify () function is triggered to re-read the verification code to refresh the verification code. The function is described as follows:
<script language="JavaScript">function changeVerify(){ var timenow = new Date().getTime(); document.getElementById('verifyImg').src='-Article/verify/'+timenow; }</script>
Verification Code
When the verification code verify is called, buildImageVerify records the MD5 information of the verification code. In form verification, use the following method to check whether the verification code is correct:
If ($ _ SESSION ['verify ']! = Md5 ($ _ POST ['verify ']) {$ this-> error ('verification code error! ');}
The verify name in $ _ SESSION ['verify '] is the default SESSION Registration Name of the buildImageVerify method. For details, see the buildImageVerify syntax.
The preceding example demonstrates how to use the simplest ThinkPHP verification code. The verification code in the preceding example is a four-digit number. For more style verification codes and Chinese verification codes, see ThinkPHP verification codes of different styles and Chinese characters.
Reason why the verification code is not displayed
The Verification Code cannot be displayed as follows:
1. Whether the GD library support has been installed in PHP.
2. Whether there is any output before the output (especially the BOM header information output of UTF8 ).
3. Whether the Image class library is correctly imported.
4. On the form page, check whether the verification code display method is correctly called.
The following describes how to generate the thinkphp3.2 verification code and click "Refresh Verification Code". The details are as follows:
1. instantiate the class that generates the verification code (this method is put in IndexController for easy access)
/*** Verification code generation */public function verify_c () {$ Verify = new \ Think \ Verify (); $ Verify-> fontSize = 18; $ Verify-> length = 4; $ Verify-> useNoise = false; $ Verify-> codeSet = '000000'; $ Verify-> imageW = 0123456789; $ Verify-> imageH = 50; // $ Verify-> expire = 600; $ Verify-> entry ();}
2. the src attribute of the image to be generated on the front-end directs
<P class = "top15 captcha" id = "captcha-container"> <input name = "verify" width = "50%" height = "50" class = "captcha-text" placeholder = "Verification Code" type = "text"> </p>
3. After the above verification code is written, the verification code initialized on the page will appear. The following is to click the verification code image, refresh the new verification code image (this is done by modifying the src attribute of the image through jquery. The request processing function is the same, but a random number is added after the request to distinguish the request of the previous image)
// Var captcha_img generated by the Verification Code =$ ('# captcha-iner '). 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 ());}});
4. Check whether the verification code is entered correctly
A. Add global functions to 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 the check code in the processing method corresponding to the controller submitted in the form.
// Check the verification code $ verify = I ('Param. verify ', ''); if (! Check_verify ($ verify) {$ this-> error ("dear, incorrect verification code! ", $ This-> site_url, 9 );}
You can use the tp3.2 verification code.
Supplement: When I write, I put Step 4 in an ajax for verification and return a verification result. Then determine whether to submit the form based on the returned results. However, after the verification code passes the first verification, the second verification is not allowed. I have not yet figured out the reason.
This is all about this article. There is a small question at the end of this article. I hope you can come up with a solution and help you learn this article.
Articles you may be interested in:
- Simple ThinkPHP Verification Code tutorial
- Thinkphp Verification Code cannot be displayed
- ThinkPHP verification code and paging instance tutorial
- ThinkPHP implements the file upload function instance with the verification code
- Perfect solution to the problem that thinkphp Verification Code fails to be displayed
- ThinkPHP on the verification code page, the solution is garbled.
- Simple use of verification codes in thinkPHP