ThinkPHP3.1 quick start (20) verification code

Source: Internet
Author: User
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:
  1. Class PublicAction extends Action {
  2. Public function verify (){
  3. Import ('org. Util. image ');
  4. Image: buildImageVerify ();
  5. }
  6. }
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:
  1. 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:
  1. Whether the GD Library is installed and enabled properly;
  2. Whether the Image class library and String class library are in the correct position and correctly imported;
  3. 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:
  1. If (session ('verify ')! = Md5 ($ _ POST ['verify ']) {
  2. $ This-> error ('verification code error! ');
  3. }
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:
  1. Public function verify (){
  2. Import ("ORG. Util. Image ");
  3. Image: GBVerify ();
  4. }
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:
  1. Function fleshVerify (){
  2. // Reload the verification code
  3. Var time = new Date (). getTime ();
  4. Document. getElementById ('verifyimg '). src =' _ APP _/Public/verify/'+ time;
  5. }
  6. Script
  7. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.