基于Zend的Captcha机制的应用_php实例
如何生成验证码图片?使用php的GD? ok,right。其实Zend的Captcha模块已经封装好了。这篇文章就说一下如何使用Zend的Captcha模块。
环境安装
首先Zend的Captcha需要安装GD。查看有没有安装GD需要去phpinfo()中看是否有GD模块。(注意,有可能出现php -m里面的模块有gd但phpInfo()里面的模块没有gd,这个问题是说明你的PHP和Apache没有安装对。具体请去google之)
(如果在安装gd的过程中提示Missing Dependency: libt1.so.5模块错误,请看这篇文章:http://www.siutung.org/post/730/)
生成验证码图片
使用Zend_Captcha_Image类
$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 这里有两个变量需要说一下,$id 和 $code。
图片文件名就是$id . ".png"; 这个id是一个随机数。
$code是这个图片中的文字,就是验证码的答案
2 setWordLen 等设置的接口是Zend_Captcha_Image暴露给外面的对验证码图片的设置。其实看函数名也能知道是做什么的了。具体请参考Zend的Api手册。
3 font字体文件必须在服务器上有,ImgDir设置的是图片生成路径
验证验证码图片
好了,生成了验证码图片,现在要验证验证码了。
验证步骤就需要用到Zend_Session_Namespace这个session存储模块。
首先,生成验证码的时候有id和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();
这里看到,我们使用$captcha_code_$id将code存储下来。目的是等到验证步骤的时候使用。
第二步
给页面传递表单的时候把$id和验证码图片传递过去。
让用户填写验证码。
第三步,验证。
验证这步需要用户提供两个参数: $id 和验证码答案$code
$codeSession = new Zend_Session_Namespace('captcha_code_' . $this->_params['id']);
if ($codeSession == null || strtolower($codeSession->code) != strtolower($this->_params['code'])) {
$this->Output(ERROR);
}
这段代码读起来很顺口吧:如果captcha_code_$id中有保存code,并且code和用户填写的code一致,那么就验证成功。
这样,验证码验证过程就结束了。
深入考虑
好了,其实验证码没有这么简单。下面有几个问题值得考虑
验证码图片是不会自动删除的,所以生成的验证码图片所在文件夹体积会不断增加。怎么办?
Image类中是提供了方法的$captcha->setGcFreq(5) 。
具体使用方法看API吧
我希望自己设置$id,怎么办?
答案是在Zend_Captche_Image上再封装一层,然后重写generate()方法
比如我重写了一个类:
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;
}
}
我希望我每个用户只有一个验证码,这个验证码的图片名称就是userid.png
那么使用这个类的代码是这样的
$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();
--------------
// 验证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);
}
附言
Zend的Captcha是封装了基本的验证码动作。生成简单的验证码基本是完全不需要看内部的代码的了,但如果你需要对验证码进行更高级的操作,比如修改验证码的显示文字等,最好就需要将Captcha的源码看一下了。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



The Writeup shared today is a simple human-computer authentication (Captcha) bypass method discovered by the author during vulnerability testing of the target website. Captcha bypass was achieved by simply editing elements on the login page of the target website using Chrome developer tools. Pass. Human-machine authentication (Captcha) usually appears on the registration, login and password reset pages of the website. The following is the Captcha mechanism arranged by the target website in the login page. As you can see from the picture above, only after the user checks "I'mnotarobot" of the Captcha verification mechanism, the login button (Sign-IN) will be enabled and displayed for the user to click. So based on this, I right clicked on Si

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

With the development and popularization of the Internet, more and more websites and applications have been developed, many of which require verification codes to ensure user validity and security. This article will introduce how to use the Beego framework and Captcha library to implement the verification code function. Among them, Beego is a web application framework based on Go language, and Captcha is an open source verification code library in Go language. Preparation work for the Beego framework. Before implementing the verification code, you need to install the Beego framework and create a new

The Windows 2003 installation package includes Zend, PHP5.2.17, PHPWind8.7 and PHPMyadmin3.5.2. You can download the installation package directly to save time searching for resources. However, since MySQL has exceeded the upload limit, you need to go to the MySQL official website to download. Then unzip and copy to the D drive, as shown below: MySQLinDdisk Install and configure WindowsIIS+FTP Click Start>Control Panel>Add or Remove Programs.AddingordeletingaPG Click Add/Remove Windows Components (A). Addingorde

PHP does not recognize ZendOptimizer, how to solve it? In PHP development, sometimes you may encounter a situation where PHP cannot recognize ZendOptimizer, which will cause some PHP codes to not run properly. In this case, we need to take some measures to solve the problem. Some possible workarounds are described below, along with specific code examples. 1. Confirm whether ZendOptimizer is installed correctly: First, we need to confirm that ZendOptimizer

With the rapid development of the Internet, graphic-based verification codes have become an important part of website security. Verification codes can effectively prevent robots or malicious programs from automating operations on the website, and can also ensure the security of user information. In website development based on ThinkPHP6, how to implement the operation of captcha graphic verification code? This article will introduce you to the specific operation process. 1. Generate Captcha graphical verification code 1. Use the captcha library to install it in ThinkPHP

With the popularity of the Internet, verification code technology has become a routine protection method for websites and applications. CAPTCHAs can prevent malicious robots and crawlers from attacking websites and applications, ensuring the security of user information and privacy. In ThinkPHP6, Captcha technology is built-in, and the verification code function can be easily implemented through simple configuration and call. 1. Basic introduction to Captcha Captcha is an image verification code technology. Its principle is to provide users with a verification code when they log in or submit a form.
