In this age of network security, the verification code function is at least a trustworthy good guard. This article describes how to use the verification code function in ThinkPHP. In this age of network security, the verification code function is at least a trustworthy good guard. This article describes how to use the verification code function in ThinkPHP.
Get extended class library ThinkPHP extensions by Image processing class Image and character processing class String used to generate random verification code together to complete the verification code function, can be in the mirror (http://www.thinkphp.cn/down/253.html) it also contains the above extension classes. If the extension class library is downloaded separately, extract the decompressed Image. class. php and String. class. put ThinkPHP/Extend/Library/ORG/Util/together with php (manually create one if not.
Because the verification code is displayed in the GD database, the environment must support the GD database.
It is actually very easy to generate a verification code. you only need to add an operation method to your Action. generally, to avoid the impact of permission control, we usually put this method in a public Action or a controller that can be accessed without authorization (for example, PublicAction). below we will put it in the PublicAction controller class as an example. the code is as follows:
- Class PublicAction extends Action {
- Public function verify (){
- Import ('org. Util. image ');
- Image: buildImageVerify ();
- }
- }
After copying the code definition, we can call the verify method of the Public module in any module that requires the verification code to display the verification code. All we need to do is add the following call code to the required template:
- Src = '_ APP _/Public/verify/'/>
Copy the code so that we can see the default verification code image after accessing this page, as shown below:
If your verification code cannot be properly displayed, check:
- Whether the GD Library is installed and enabled properly;
- Whether the Image class library and String class library are in the correct position and correctly imported;
- Whether there are any other outputs before the verification code is output (especially the BOM header information output of UTF8 );
Verification codes cannot be displayed by many developers. most of them are output files or BOM headers. tools can be used to detect the BOM header of UTF8, many editors also support saving BOM removed (if this is not clear, you can use Baidu ). It should be emphasized that many developers are not standardized enough to write code. for example, the following situations often occur:
Empty line at the beginning of the file
Empty lines at the end of the file
In both cases, other outputs on the page are generated, resulting in an error in the verification code.
By default, the verification code is displayed with four random digits. you can set different display modes by using parameters. the buildImageVerify method of the Image class is used as follows:
Image Verification code generated by buildImageVerify |
Usage |
BuildImageVerify ($ length, $ mode, $ type, $ width, $ height, $ verifyName) |
Parameters |
Length |
The length of the verification code. the default value is 4 digits. |
Model |
Verify the string type. the default value is a number. other supported types include 0 letters, 1 digits, 2 uppercase letters, 3 lowercase letters, 4 Chinese characters, and 5 characters. |
Type |
The image type of the verification code. the default value is png. |
Width |
The verification code width. it is automatically calculated by default based on the verification code length. |
Height |
Height of the verification code. the default value is 22. |
VerifyName |
The SESSION record name of the verification code. the default value is verify. |
Each time the verification code is entered, the md5 string information of the verification code is recorded through the SESSION. Therefore, check whether the verification code is correct, we only need to use the following code in the Action to judge:
- If (session ('verify ')! = Md5 ($ _ POST ['verify ']) {
- $ This-> error ('verification code error! ');
- }
We recommend that you use the session method to obtain the SESSION value. because the session method is used to save the value in the verification code generation method, errors may occur due to the impact of the session prefix. The verify name depends on the verifyName parameter value of your verification code.
If your session function is abnormal, the verification code may report an error.
The Chinese verification code BuildImageVerify method does not support displaying Chinese verification codes. to display Chinese verification codes, use the GBVerify method. Usage example:
- Public function verify (){
- Import ("ORG. Util. Image ");
- Image: GBVerify ();
- }
The result of copying the code is as follows:
If the image can be displayed but the Chinese characters in the image are not displayed, check whether the font file is copied to the directory where the image Class Library is located. The default font file is simhei. ttf (this file can be found in the Fonts directory of the window ).
The GBVerify method also has different parameters for setting verification codes. the specific usage is as follows:
GBVerify generates a Chinese verification code |
Usage |
GBVerify ($ length, $ type, $ width, $ height, $ fontface, $ verifyName) |
Parameters |
Length: Length of the verification code. the default value is 4 digits. Type: The image type of the verification code. the default value is png. Width: The verification code width. it is automatically calculated based on the verification code length by default. Height: Height of the verification code. the default value is 50. Fontface: The font file used. use the complete file name or put it under the Directory where the image class is located. the default font file used is simhei. ttf (this file can be found under the Fonts directory of the window ). VerifyName: The SESSION record name of the verification code. the default value is verify.
|
If the randomly generated verification code cannot be clearly displayed, you need to add the verification code refresh function to re-generate it. In fact, this is only to modify the front-end code, and the background verification code generation method does not need to be modified.
Generally, we use the js method to control the refresh of the verification code. for example, we can click the verification code Image to refresh the verification code:
-
- Function fleshVerify (){
- // Reload the verification code
- Var time = new Date (). getTime ();
- Document. getElementById ('verifyimg '). src =' _ APP _/Public/verify/'+ time;
- }
- Script
- Id = "verifyImg" src = '_ APP _/Public/verify/'onclick = "fleshVerify ()"/>
Of course, if you are familiar with Jquery, you can use Jquery code to simplify the js method for refreshing verification codes.