代码改变世界

Zend的Captcha机制

2012-08-13 18:31  轩脉刃  阅读(2249)  评论(0编辑  收藏  举报

如何生成验证码图片?使用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的源码看一下了。

 

参考资料

Zend的Captcha的官方手册在这:

http://framework.zend.com/manual/1.11/zh/zend.captcha.html

关于captcha有这么几篇文章:

http://mnshankar.wordpress.com/2009/08/13/understanding-zend-captcha-zend_captcha_image/

http://stackoverflow.com/questions/6464657/how-to-create-captcha-in-zend-framework

http://www.krmaurya.com/13/hello-world/

http://www.hardcode.nl/subcategory_4/article_605-zend-captcha-image-example