Zend-based Captcha-based app _ PHP Tutorial

Source: Internet
Author: User
Tags api manual
Zend-based Captcha mechanism. How do I generate a verification code image? Use php GD? OK, right. In fact, the Zend Captcha module has been encapsulated. This article describes how to use the Zend Captcha module. How does one generate a verification code image during environment installation? Use php GD? OK, right. In fact, the Zend Captcha module has been encapsulated. This article describes how to use the Zend Captcha module.


Environment installation
First, GD must be installed for Zend Captcha. Check whether the GD module is installed in phpinfo. (Note: The module in php-m may contain gd, but the module in phpInfo () does not have gd. this problem indicates that your PHP and Apache are not correctly installed. Go to google for details)

(If Missing Dependency: libt1.so. 5 module error is prompted during gd installation, please refer to this article: http://www.siutung.org/post/730)


Generate verification code Image
Use the Zend_Captcha_Image class

The code is as follows:


$ Captcha = new Zend_Captcha_Image ();
$ Captcha-> setWordLen ('4 ')
-> SetHeight ('60 ')
-> SetFont (NCHANNEL_FONT_DIR. '/arial. ttf ')
-> SetImgDir (NCHANNEL_CAPTCHA_DIR)
-> SetDotNoiseLevel ('5 ')
-> SetLineNoiseLevel ('5 ');

$ Id = $ captcha-> generate ();

$ Code = $ captcha-> getWord ();


1There are two variables, $ id and $ code.

The image file name is $ id. ". png"; this id is a random number.

$ Code is the text in this image, which is the answer to the verification code.

2SetWordLen and other settings are the settings of the verification code Image exposed to the outside by Zend_Captcha_Image. In fact, you can also know what the function name is. For more information, see The Zend Api manual.

3Font files must be available on the server, and ImgDir sets the image generation path.

Verification code Image
Now we have generated a verification code image and want to verify it.

The session storage module Zend_Session_Namespace is required for verification.


First, the id and code variables should be saved when the verification code is generated.
Okay. go back to the previous step and modify the code.

The code is as follows:


$ Captcha = new Zend_Captcha_Image ();
$ Captcha-> setWordLen ('4 ')
-> SetHeight ('60 ')
-> SetFont (NCHANNEL_FONT_DIR. '/arial. ttf ')
-> SetImgDir (NCHANNEL_CAPTCHA_DIR)
-> SetDotNoiseLevel ('5 ')
-> SetLineNoiseLevel ('5 ');

$ Id = $ captcha-> generate ();
$ CodeSession = new Zend_Session_Namespace ('captcha _ code _ '. $ id );

$ CodeSession-> code = $ captcha-> getWord ();


We can see that $ captcha_code _ $ id is used to store the code. The purpose is to wait until the verification step is used.

Step 2
When you pass the form to the page, pass the $ id and verification code image.

Ask the user to enter the verification code.

Step 3: verify.
To verify this step, you need to provide two parameters: $ id and verification code answer $ code

The code is as follows:


$ CodeSession = new Zend_Session_Namespace ('captcha _ code _ '. $ this-> _ params ['id']);
If ($ codeSession = null | strtolower ($ codeSession-> code )! = Strtolower ($ this-> _ params ['code']) {
$ This-> Output (ERROR );

}


This code is easy to read: if the captcha_code _ $ id contains the saved code and the code is the same as the code entered by the user, the verification is successful.


In this way, the verification process is over.


In-depth consideration
Well, the verification code is not that simple. The following issues are worth considering:

The verification code image is not automatically deleted, so the size of the folder where the generated verification code Image is located will increase. What should I do?
The $ captcha-> setGcFreq (5) method is provided in the Image class ).

See the API for specific usage.


What should I do if I want to set $ id on my own?
The answer is to encapsulate a layer on Zend_Captche_Image and then override the generate () method.


For example, I have rewritten a class:

The code is as follows:


Class Test_Captcha_Image extends Zend_Captcha_Image
{
Protected $ _ fid = "";

Public function generate ()
{
$ Word = $ this-> _ generateWord ();
$ This-> _ setWord ($ word );
If ($ this-> _ fid ){
$ Id = $ this-> _ fid;
}
$ This-> _ generateImage ($ id, $ this-> getWord ());

If (mt_rand (1, $ this-> getGcFreq () = 1 ){
$ This-> _ gc ();
}
Return $ id;
}

Public function setId ($ id ){
$ This-> _ fid = $ id;
Return $ this;
}
}


I hope that each user has only one verification code. this verification code is called "userid.png ".

The code for using this class is as follows:

The code is as follows:


$ Captcha = new Test_Captcha_Image ();
$ Captcha-> setWordLen ('4 ')
-> SetHeight ('60 ')
-> SetFont (NCHANNEL_FONT_DIR. '/arial. ttf ')
-> SetImgDir (NCHANNEL_CAPTCHA_DIR)
-> SetDotNoiseLevel ('5 ')
-> SetLineNoiseLevel ('5 ')
-> SetId ($ user_id );

$ Id = $ captcha-> generate ();
$ CodeSession = new Zend_Session_Namespace ('captcha _ code _ '. $ user_id );
$ CodeSession-> code = $ captcha-> getWord ();

--------------
// Verify the session
$ CodeSession = new Zend_Session_Namespace ('captcha _ code _ '. $ this-> _ params ['User _ id']);
If ($ codeSession = null | strtolower ($ codeSession-> code )! = Strtolower ($ this-> _ params ['code']) {
$ This-> Output (ERROR );
}


Postscript
Zend Captcha encapsulates basic verification code actions. To generate a simple verification code, you do not need to look at the internal code at all. However, if you need to perform more advanced operations on the verification code, such as modifying the display text of the verification code, it is best to take a look at the Captcha source code.

Why? Use php GD? OK, right. In fact, the Zend Captcha module has been encapsulated. This article describes how to use the Zend Captcha module. Environment installation...

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.