Application of Captcha mechanism based on Zend

  • 2020-06-01 09:15:19
  • OfStack

How to generate captcha images? GD using php? ok right. In fact, the Captcha module of Zend has been packaged. This article is about how to use Zend's Captcha module.


Environmental installation
First, Zend's Captcha needs to be installed with GD. To see if GD is installed, you need to go to phpinfo() to see if there is an GD module. Note that it is possible that the module in php-m has gd, but the module in phpInfo() does not have gd. This problem indicates that your PHP and Apache are not installed correctly. Please go to google for details.)

(if in the process of installing gd prompt Missing Dependency: libt1. so. 5 module error, please look at this article: http: / / www siutung. org/post / 730 /)


Generate captcha images
Using Zend_Captcha_Image class


$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 1, $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 captcha

2. The interface of setWordLen and other Settings is the setting of Zend_Captcha_Image exposed to the captcha image. You can actually see what it's doing by looking at the name of the function. Please refer to the Api manual of Zend for details.

3. font font files must be available on the server. ImgDir is set to the image generation path

Verify the captcha image
Ok, we've generated the captcha image, now we need to verify the captcha.

The verification step requires the Zend_Session_Namespace, the session storage module.


First, the variables id and code should be saved when generating the captcha.
Ok, go back to step 1 and change the code


$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 you can see here, we store code with $captcha_code_$id. The goal is to wait until the validation step.

Step 2
Pass the $id and captcha image to the page when you pass the form.

Let the user fill in the captcha.

Step 3, verify.
Validation requires the user to provide two parameters for this step: $id and $code for the captcha answer


$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 captcha_code_$id saves code, and code matches the code1 the user filled in, the validation is successful.


This completes the captcha verification process.


Further consideration
Well, the captcha is not that simple. Here are a few questions to consider

Captcha images are not automatically deleted, so the generated captcha images folder volume will continue to increase. How to do?
In the Image class is the $captcha- that provides the method > setGcFreq (5).

See API for specific usage methods


I want to set $id myself. What should I do?
The answer is to wrap a layer on top of Zend_Captche_Image and override the generate() method


Let's say I rewrite a class:


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 only want one captcha per user, and the image name of this captcha is userid.png

So the code that USES this class looks like this


$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(); 

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

P.S.
Zend's Captcha encapsulates the basic captcha action. Generating a simple captcha basically doesn't require looking at the internal code at all, but if you need to perform more advanced operations on the captcha, such as modifying the captcha's display text, it is best to look at the source code of Captcha.


Related articles: