Application of CAPTCHA mechanism based on Zend _php example

Source: Internet
Author: User
Tags api manual php and phpinfo zend

How do I generate a captcha picture? Using PHP's GD? Ok,right. In fact, Zend's CAPTCHA module has been encapsulated. This article is about how to use the Zend Captcha module.


Environment installation
First Zend Captcha need to install GD. Check to see if there is a GD module installed to Phpinfo (). (Note that there may be php-m inside the module has GD but Phpinfo () inside the module is not GD, the problem is that your PHP and Apache are not installed on. Specifically please go to Google)

(If you are prompted to missing dependency:libt1.so.5 module errors during the installation of GD, see this article: http://www.siutung.org/post/730/)


Generate Captcha Picture
Using the Zend_captcha_image class

Copy Code code 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 ();

1Here are two variables to say, $id and $code.

Picture 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 Setwordlen settings such as the interface is zend_captcha_image exposed to the outside of the validation code picture settings. In fact, look at the function name can also know what to do. Please refer to Zend's API manual for details.

3 Font font file must be on the server, Imgdir set is the picture generation path

Verify Code Picture
OK, I have generated the code picture, now I want to verify the verification code.

The verification step requires the use of the Zend_session_namespace session enclosure.


First, the ID and code two variables should be saved when the CAPTCHA is generated.
All right, go back to the previous step and make a change to the code.

Copy Code code 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 code. The purpose is to wait until the verification step is used.

Second Step
Pass the $id and the Captcha picture to the page when you pass the form.

Let the user fill out the verification code.

Step three, verify.
Validation This step requires the user to provide two parameters: $id and verification Code answer $code

Copy Code code 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 very easy to read: If there is a save code in the Captcha_code_$id and code is consistent with the code that the user fills in, then the validation succeeds.


In this way, the verification code verification process is over.


In-depth consideration
Well, the verification code is not so simple. Here are a few questions worth considering

The verification code picture is not automatically deleted, so the generated verification Code picture folder volume will continue to increase. What to do?
The image class is a $captcha->setgcfreq (5) that provides a method.

Use the method to see the API.


I want to set up $id myself, how to do?
The answer is to encapsulate a layer on the zend_captche_image, and then rewrite the Generate () method


For example, I rewrote a class:

Copy Code code 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 I have only one authentication code per user, the image name of this verification code is userid.png

So the code to use this class is this

Copy Code code 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 ();

--------------
Validate 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
The captcha of the Zend is encapsulated with the basic Verification Code action. Generate a simple verification code is basically do not need to look at the internal code, but if you need to do more advanced verification code, such as the code to modify the display text, and so on, it is best to captcha the source to see.

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.