zend 验证码插件运用

 
Zend_Captcha生成验证码实例
2008-10-15 10:21
老规矩 先看例子


首先是CONTROLLER里的代码

//生成随即验证码
      $this->codeSession = new Zend_Session_Namespace('code'); //在默认构造函数里实例化
     
      $captcha = new Zend_Captcha_Image(array('font'=>'./public/images/faktos.ttf', //字体文件路径
               'fontsize'=>24, //字号
               'imgdir'=>'./public/images/code/', //验证码图片存放位置
               'session'=>$this->codeSession, //验证码session值
               'width'=>120, //图片宽
               'height'=>50,   //图片高
               'wordlen'=>5 )); //字母数
              
      $captcha->setGcFreq(3); //设置删除生成的旧的验证码图片的随机几率
      $captcha->generate(); //生成图片
      $this->view->ImgDir = $captcha->getImgDir();
      $this->view->captchaId = $captcha->getId(); //获取文件名,md5编码
      $this->codeSession->code=$captcha->getWord(); //获取当前生成的验证字符串
     
      echo $this->codeSession->code;

----------------------------------------------

view页面调用代码

<span id="captcha"><img src="<?php echo $this->ImgDir,$this->captchaId ?>.png" border="0" />



值得注意的是。
系统默认的验证图片躁点较多
如果需要修改躁点个数

编辑 Zend/Captcha/Image.php

393行 ,454行 两个地方同时改动红色数字 降低躁点
// generate noise
       for ($i=0; $i< 100; $i++) {
          imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
       }

最让人想不通的是 Zend居然每刷新一次 就生成一张图片 并且存放到 $imgdir 下面

网上有朋友已经修改过源码 采用header方式.

在这里可以设定一个图片数量 超过限制就自动删除。 如上代码所示 $captcha->setGcFreq(3)

另外需要修改 protected function _gc() 函数为:

protected function _gc()
    {
        //$expire = time() - $this->getExpiration();
        $expire = time();
        echo $this->getExpiration();
        foreach (new DirectoryIterator($this->getImgDir()) as $file) {
            if (!$file->isDot() && !$file->isDir()) {
                if ($file->getMTime() < $expire) {
                    unlink($file->getPathname());
                }
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值