ThinkPHP has built-in support for verification codes and can be used directly. To use the verification code, you need to import the ORG. Util. Image class library in the extended class library and the ThinkPHP verification code library ORG. Util. String.
ThinkPHP has built-in support for verification codes and can be used directly. To use the verification code, you must import ORG. Util. Image from the extended class library.
Class library and ORG. Util. String class library.
Verification code method
We add a verify
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 ThinkPHP.
Built-in class libraries and file import methods. the file imported in the previous example is the Lib/ORG/Util/Image. class. php file in the ThinkPHP system directory. If the Image
The class library is copied to the current project, such as Lib/ORG.
, You can use:
Import ("@. Util. Image ");
For more information, see ThinkPHP import
Class Library import.
The import method is the built-in class library and file import method of ThinkPHP. the file imported in the preceding example is in the ThinkPHP system directory.
Lib/ORG/Util/Image. class. php
File.
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:
ThinkPHP verification code example
Use verification code in the form
Use the verification code on the form page, which is
Html img label to call:
Id = "verifyImg" src = "_ URL _-verify" onClick = "changeVerify ()" title = "click to refresh the verification code"
/>
The src attribute value is the access address of the verification code method, which varies depending on the actual situation.
Refresh verification code
Triggered when you click the verification code image.
The JavaScript changeVerify () function re-reads the verification code to refresh the verification code. The function is described as follows:
Language = "JavaScript">
Function changeVerify (){
Var timenow = new
Date (). getTime ();
Document. getElementById ('verifyimg '). src =' _ URL _/verify/'+ timenow;
}
Script
Verification code
Call the verification code
During verify, buildImageVerify records the MD5 value of this verification code.
Information. 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! ');
}
Where
The verify name in $ _ SESSION ['verify '] is the default SESSION registration name of the buildImageVerify method. for details, see
BuildImageVerify syntax.
The preceding example demonstrates how to use the simplest ThinkPHP verification code. The verification code in the above example is 4
For more style verification codes and Chinese verification codes, see ThinkPHP
Use verification codes of different styles and Chinese characters.
Reason why the verification code is not displayed
The verification code cannot be displayed as follows:
PHP has GD installed?
Library Support. For more information about the GD library, see the GD library section of PHP image processing.
Whether there is any output before the output (especially the UTF8 BOM)
Header information output ).
Whether the Image Class Library is correctly imported.
On the form page, check whether the verification code display method is correctly called.