Thinkphp 3.1 QuickStart: Verification Code

Source: Internet
Author: User
Keywords QuickStart thinkphp
Tags class code display download file function gd library how to

In the era of cyber-security, the verification code function can at least be regarded as a trustworthy good guard. In this article, we'll talk about how to use the CAPTCHA function in thinkphp.

Get Extended Class Library

The thinkphp extensions are performed with the image processing class image and the character processing class string used to generate the random CAPTCHA code to complete the verification code function in the http://www.thinkphp.cn/extend/225. HTML download image processing class and http://www.thinkphp.cn/extend/266.html download character processing class, or download the official Full expansion pack (http://www.thinkphp.cn/down/253. HTML) contains the above extended classes. If you are downloading an extended class library separately, put the extracted Image.class.php and String.class.php together in the thinkphp/extend/library/org/util/(if not manually created) directory below. Because the verification code display uses the GD library support, therefore needs the environment to support the GD library.

Generate Authenticode

Generating a CAPTCHA is simply a matter of adding an action method to your action, generally in order to avoid the influence of permission control, we usually put this method into a public action or a controller (such as publicaction) that can be accessed without authorization. Let's take this as an example in the Publicaction controller class, where the code is as follows:

Class Publicaction extends action{public Function verify () {Import (' ORG.          Util.image ');      Image::buildimageverify (); }  }

Once defined, we can invoke the Verify method of the public module in any module that requires a CAPTCHA to display the verification code, and only need to add the following calling code to the template that is required:

<img src= ' __app__/public/verify/'/>

This way, when we visit the page, we can see the default CAPTCHA image display, as follows:

If your verification code does not display properly, please check:

Whether the GD library has been installed to support and open normally;

The Image class library and the String class library are in the correct location and are imported correctly;

Verify code output before any other output (especially UTF8 BOM header information output);

Many developers verify that the code can not be displayed in most cases because there is output or BOM header in the file, about UTF8 's BOM head detection tools can be detected, many editors also support the removal of the BOM save (this is not clear words can be Baidu). It is also emphasized that many developers are not spec-coded when writing code, for example:
There are blank lines at the beginning of the file

There is a blank line at the end of the file

Both of these situations result in additional output from the page, which results in a validation code error.

Related Article

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.