thinkphp (SAE) Call verification code does not invoke code correctly

Source: Internet
Author: User

Now, the method of the Generals network is posted:

Use thinkphp Verification code under SAE, non-Saevcode

Saevcode in fact very weak, even the size of the verification code can not be set, for development and art, is a very headache, after our technician simple test, found that the thinkphp comes with the verification code can be normal operation under the SAE.

1. Copy "thinkphp\extend\library\org\util\string.class.php" to "
Thinkphp\extend\engine\sae\lib\extend\library\org\util\string.class.php ";

2, delete "thinkphp\extend\engine\sae\lib\extend\library\org\util\image_sae.class.php" in the "buildimageverify" function;

3. Copy "buildimageverify" function in "thinkphp\extend\library\org\util\image.class.php" to "thinkphp\extend\engine\sae\lib\" Extend\library\org\util\image_sae.class.php "can.

Verification methods and code generation are not modified, as is the case with local development.
For the developer's reference, error please advise!

Because the author is just using TP novice, so looked at the next:

ThinkPHP3.1 Quick Start (20) Verification code

In this era of network security, the verification code function at least can be regarded as a trustworthy good guard. In this article we will talk about how to use the Captcha feature in thinkphp.

Get Extension Class Library

The thinkphp extension consists of the image processing class image and the character-processing string used to generate the random code to perform the verification code function, which can be http://www.thinkphp.cn/extend/225. The HTML download image processing class and http://www.thinkphp.cn/extend/266.html download the character processing class, or download the official Full expansion pack (http://www.thinkphp.cn/down/253. HTML) also contains the above extension classes. If it is a separate download of the Extension class library, put the extracted Image.class.php and String.class.php together into thinkphp/extend/library/org/util/(if not manually created) under the directory. because the verification code display is supported by the GD library, the Environment Support GD Library is required.

Generate Verification Code

Generating a verification code is very simple, just add the action method in your action, generally to avoid the effect of permission control, we usually put this method to public action or not authorized to access the controller (such as Publicaction), Let's take the example of the Publicaction controller class, with the following code:

  1. Class publicaction extends Action{
  2. public function Verify() {
  3. Import(' ORG. Util.image ');
  4. Image::buildimageverify();
  5. }
  6. }
Copy Code

Once defined, we can invoke the Verify method of the public module in any module that requires verification code to display the verification code, just add the following calling code to the template you need:

    1. src='!-app-!/public/verify/' />
Copy Code

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

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

    1. If you have installed GD library support and open normally;
    2. Whether the Image class library and the String class library are in the correct location and are imported correctly;
    3. Verify that there are any other outputs before the code output (especially the UTF8 BOM header information output);

Many developers verification code can not display the situation is mostly due to the output or BOM header in the file, about UTF8 of the BOM head detection tools can be detected, many editors also support the removal of the BOM save (this is not clear if you can Baidu). It is also emphasized here that many developers are not prescriptive enough to write code, for example, often the following situations occur:
There is a blank line at the beginning of the file

There is a blank line at the end of the file

Both of these conditions result in additional output from the page, resulting in an error in the CAPTCHA.

Verification Code Display Settings

By default, the CAPTCHA uses a random 4-digit number display, we can set the different display way through the parameter, the image class's Buildimageverify method uses as follows:

Buildimageverify Generate image Verification code
Usage Buildimageverify ($length, $mode, $type, $width, $height, $verifyName)
Parameters Length The length of the verification code, which defaults to 4 digits
Model Verify string type, default to number, other support type has 0 letter 1 digit 2 Capital Letter 3 Lowercase Letter 4 Chinese 5 mix
Type The picture type of the CAPTCHA, which is PNG by default
Width The width of the verification code is automatically calculated based on the verification code length by default
Height The height of the verification code, which defaults to 22
VerifyName The session record name of the verification code, which defaults to verify

Validate input

Each time the verification code is generated, it will be recorded through the session of the verification code after the MD5 string information, so, to check whether the verification code is correct, we only need to use the following code in the action to determine the line:

    1. If(session(' verify ')! = MD5($_post[' verify '])) {
    2. $this, error (' captcha is wrong! ');
    3. }
Copy Code

It is recommended to use the session method to get the session value, because the validation code generation method is also used to save the session method, you can avoid the effect of the session prefix and error. The verify name here depends on the value of the VerifyName parameter of your verification code.
If your session does not function properly, it may cause the verification code to detect an error in the event.

Chinese Verification Code

The Buildimageverify method does not support the display of Chinese code, and if you need to display a Chinese verification code, use the Gbverify method, using the example:

    1. Public function Verify() {
    2. Import("ORG. Util.image ");
    3. Image::gbverify();
    4. }
Copy Code

The display results are as follows:

If you can display the picture, but you can't see the Chinese characters in the picture, check to see if you have copied the font file to the directory where the Image class library is located. The font file used by default is Simhei.ttf (this file can be found under the Fonts Directory in window).
The Gbverify method also has different parameters for setting the verification code, as follows:

Gbverify Generate Chinese verification Code
Usage Gbverify ($length, $type, $width, $height, $fontface, $verifyName)
Parameters Length: Number of verification codes, default to 4 digits
Type: A picture of the CAPTCHA, default to PNG
Width: Verification code, the default is automatically based on the length of the verification code automatically calculated
Height: The high of the Verification Code, which defaults to 50
fontface: The font file used, using the full file name or placed under the directory where the image class resides, the default font file used is Simhei.ttf (the file can be found under the Fonts Directory of window
VerifyName: The session record name of the verification code, the default is verify

Verification Code Refresh

If the random generated verification code can not see clearly, you need to add a code refresh function to regenerate, this is just to modify the front-end code, the background verification code generation method without modification.
In general, we are using the JS method to control the validation code refresh, such as the following can be achieved by clicking the Verification Code image Refresh Verification code:

  1. <script Language="JavaScript">
  2. function fleshverify() {
  3. //Reload Verification Code
  4. var time = new Date(). GetTime();
  5. Document. getElementById(' verifyimg '). src= '!-app-!/public/verify/'+time;
  6. }
  7. </script>
  8. id="verifyimg" src='!-app-!/public/verify/' onclick= "fleshverify()" />
Copy Code

Of course, developers who are familiar with jquery can use jquery code to simplify the JS method of validation code refresh, which is not much to say.

After reading these two articles, we begin to invoke the verification code under the SAE:

First, replace the function and the file according to the first article;

Then, call the code as it would normally be called:

<?php  class Publicaction extends action{public  function Verify () {Import (' ORG. Util.image_sae '); Image::buildimageverify ();}  ? >

Template page Invocation:

      <form action= "" method= "post" >        <p> User name: <input type= "text" name= "user"/></p>        <p > Password: <input type= "password" name= "pwd"/></p>        <p> Verification Code: <input type= "text" Name= "Checkcode" /></p>        <p><input name=" Submit "type=" Submit " Value= "Login"/><input type= "reset" value= "Cancel"/></p>      </form>

But here's the problem:

Picture does not display properly, view image

By the above code also know, Publicaction is inherited Action base class, so, open thinkphp/lib/core/action.class.php, look for the image class, not found!!!

Then, open Image_sae.class.php, which contains the class, but also said: Import function, no import success!

So, I started to see if the import path referenced correctly, found the app/org/image.class.php file, and then modified the code as follows:

<?php  class Publicaction extends action{public  function Verify () {Import (' app/org. Image '); Image::buildimageverify ();}  }? >

Results:

Problem solving, when others do not bird you, you should Baidu, you try!

thinkphp (SAE) Call verification code does not invoke code correctly

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.