How to generate verification code image? GD using php? OK, right. In fact, Zend's Captcha module has been packaged. This article will talk about how to use Zend’s Captcha module.
Environment installation
First of all, Zend’s Captcha needs to install GD. To check whether GD is installed, you need to go to phpinfo() to see if there is a GD module. (Note that it is possible that the module in php -m has gd but the module in phpInfo() does not. This problem means that your PHP and Apache are not installed correctly. Please google for details)
(If you are prompted with Missing Dependency: libt1.so.5 module error during the installation of gd, please read this article: http://www.siutung.org/post/730/)
Generate verification code image
Use Zend_Captcha_Image class
Copy 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();
$code = $captcha->getWord();
1 There are two variables that need to be mentioned here , $id and $code.
The image file name is $id. ".png"; this id is a random number.
$code is the text in this picture, which is the answer to the verification code
2 The setWordLen and other settings interfaces are Zend_Captcha_Image’s settings for verification code images exposed to the outside. In fact, you can know what the function does by looking at its name. Please refer to Zend's API manual for details.
3 The font file must be on the server, and ImgDir is set to the image generation path
Verify the verification code image
Okay, the verification code image is generated, now it is time to verify the verification code.
The verification step requires the use of the Zend_Session_Namespace session storage module.
First of all, when generating the verification code, there are two variables, id and code, that should be saved.
Okay, go back to the previous step and modify the code
Copy 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();
Here we see that we use $captcha_code_$id to store the code. The purpose is to wait until the verification step to use it.
The second step
When passing the form to the page, pass the $id and verification code image.
Let the user fill in the verification code.
The third step is verification.
This step of verification requires the user to provide two parameters: $id and verification code answer $code
Copy 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 reads very Let’s make it easy: If there is a code saved in captcha_code_$id, and the code is consistent with the code filled in by the user, then the verification is successful.
In this way, the verification code verification process is over.
Think deeply
Okay, actually the verification code is not that simple. Here are a few questions worth considering
Verification code images will not be automatically deleted, so the size of the folder where the generated verification code images are located will continue to increase. what to do?
The Image class provides the method $captcha->setGcFreq(5).
Please see the API for specific usage methods
I want to set $id myself, what should I do?
The answer is to encapsulate another layer on Zend_Captche_Image, and then rewrite the generate() method
For example, I rewrote a class:
Copy the code 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());
$this->_gc();
; ;
return $this; 🎜>
Then the code to use this class is like this
Copy the code
The code is as follows:
$captcha = new Test_Captcha_Image( ; ;setImgDir(NCCHANNEL_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 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);
}
PS
Zend’s Captcha encapsulates basic verification code actions. Generating a simple verification code basically does not require looking at the internal code, but if you need to perform more advanced operations on the verification code, such as modifying the display text of the verification code, etc., it is best to take a look at the source code of Captcha. .
http://www.bkjia.com/PHPjc/326861.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/326861.html
TechArticle
How to generate a verification code image? GD using php? OK, right. In fact, Zend's Captcha module has been packaged. This article will talk about how to use Zend’s Captcha module. Environment installation...