ThinkPHP verification code details and examples

Source: Internet
Author: User
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:




  1. Public function verify (){
  2. // Import the Image Class Library
  3. Import ("ORG. Util. Image ");
  4. Image: buildImageVerify ();
  5. }

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.

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.