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.
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
Copy codeThe 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.
Copy codeThe 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
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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.