Application of Captcha mechanism based on Zend_PHP tutorial
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
$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
$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
$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:
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:
->setDotNoiseLevel('5')
->setLineNoiseLevel('5') ->setId($user_id);
$id = $captcha->generate();
$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
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...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
