Application of CAPTCHA mechanism based on Zend _php tutorial

Source: Internet
Author: User
Tags api manual phpinfo
How do I generate a captcha picture? Using the PHP gd? Ok,right. In fact, Zend's CAPTCHA module has been packaged well. This article explains how to use the Zend Captcha module.


Environment installation
First Zend The captcha need to install GD. 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 does not have GD, this problem is stating that your PHP and Apache are not installed right. Please go to Google for details)

(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 a Captcha picture
Using the Zend_captcha_image class
Copy the 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 ();

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

The picture file name is $id. ". png"; This ID is a random number.

$code is the text in this picture, the answer to the verification code

2 Setwordlen settings such as the interface is zend_captcha_image exposed to the outside of the verification 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 fonts files must be available on the server, Imgdir set is the picture generation path

Verify the CAPTCHA picture
OK, the captcha image is generated, and the verification code is now validated.

The validation step requires the Zend_session_namespace Session storage module.


First, the ID and code two variables should be saved when generating the verification code.
Well, go back to the previous step and change the code.
Copy the 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 ();

As we can see here, we use $captcha_code_$id to store code. The goal is to wait until the verification step is used.

Step Two
Pass the $id and verification code pictures to the page when passing the form.

Let the user fill in the verification code.

Step three, verify.
Verify that this step requires the user to provide two parameters: $id and captcha answer $code
Copy the 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 Captcha_code_$id, and code and the user fill in the same code, then the validation succeeds.


This verifies that the CAPTCHA verification process is over.


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

Verification code pictures are not automatically deleted, so the generated Captcha Picture folder volume will continue to increase. What to do?
The image class is the $captcha->setgcfreq (5) that provides the method.

See API for specific usage


I want to set $id myself, what should I 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 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 wish I had only one verification code per user, and the image name of this captcha is Userid.png

So the code that uses this class is like this.
Copy the 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 ();

--------------
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);
}

Postscript
The captcha of Zend is the encapsulation of basic verification code actions. To generate a simple verification code is basically no need to look at the internal code, but if you need to do more advanced verification code, such as modifying the verification code display text, and so on, it is best to CAPTCHA source code to look at.

http://www.bkjia.com/PHPjc/326861.html www.bkjia.com true http://www.bkjia.com/PHPjc/326861.html techarticle How do I generate a captcha picture? Using the PHP gd? Ok,right. In fact, Zend's CAPTCHA module has been packaged well. This article explains how to use the Zend Captcha module. Environment installation ...

  • Related Article

    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.