Many system logins have verification code, and if you use the thinkphp framework to build a website, verification code generation and validation is easier
1. Generate the Verification code
Thinkphp a method for generating verification codes
To use a verification code, you need to import the ORG.Util.Image class library and the ORG.Util.String class library in the Extended class library. We use a verify method in the module class to display the verification code:
- Public function Verify() {
- Import(' ORG. Util.image ');
- Image:: Buildimageverify ();
- }
2. Validation code
- if ($_session[ ' verify ' ] != md5 ($_post[
- $this ->error ( ' CAPTCHA error!) '
- }
Note that the verify name here depends on the value of the VerifyName parameter of your verification code. Buildimagesverify method does not support the display of Chinese verification code, if you need to display the Chinese code, please use
Gbverify method, the parameters are as follows:
Usage examples:
- Public function Verify() {
- Import("ORG. Util.image ");
- Image::gbverify();}
The display effect is as follows
If the verification code cannot be displayed, please check:
Whether PHP has installed the GD library support;
Whether there is any output before the output (especially the UTF8 BOM header information output);
Whether the Image class library is imported correctly;
If it is a Chinese code check if there is a copy of the font file to the directory where the class library;
Using thinkphp to implement verification code function