改写验证码类,实现加法验证码

浏览:1743 发布日期:2017/01/05 分类:功能实现 关键字: 加法验证码 算法验证码 验证码
改写验证码类,实现加法验证码
使用TP自带的验证码类,改写实现加法验证码及验证。/**
     * 加法验证码
     */
    public function add_entry($id=''){
        // 图片宽(px)
        $this->imageW || $this->imageW = $this->length*$this->fontSize*1.5 + $this->length*$this->fontSize/2;
        // 图片高(px)
        $this->imageH || $this->imageH = $this->fontSize * 2.5;
        // 建立一幅 $this->imageW x $this->imageH 的图像
        $this->_image = imagecreate($this->imageW, $this->imageH);
        // 设置背景
        imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]);

        // 验证码字体随机颜色
        $this->_color = imagecolorallocate($this->_image, mt_rand(1,150), mt_rand(1,150), mt_rand(1,150));
        // 验证码使用随机字体
        $ttfPath = dirname(__FILE__) . '/Verify/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';

        if(empty($this->fontttf)){
            $dir = dir($ttfPath);
            $ttfs = array();
            while (false !== ($file = $dir->read())) {
                if($file[0] != '.' && substr($file, -4) == '.ttf') {
                    $ttfs[] = $file;
                }
            }
            $dir->close();
            $this->fontttf = $ttfs[array_rand($ttfs)];
        }
        $this->fontttf = $ttfPath . $this->fontttf;

        if($this->useImgBg) {
            $this->_background();
        }

        if ($this->useNoise) {
            // 绘杂点
            $this->_writeNoise();
        }
        if ($this->useCurve) {
            // 绘干扰线
            $this->_writeCurve();
        }

        // 绘验证码
        $code = array(); // 验证码
        $text_str = '';
        $sum      = 0;
        for($i=0;$i < $this->length;$i++){
            $tmp= mt_rand(1,10);
            if($i == $this->length-1 ){
                $text_str .= $tmp.'=';
                $sum      += $tmp;
            }else{
                $text_str .= $tmp.'+';
                $sum      += $tmp;
            }
        }
        $code = $text_str;
        imagettftext($this->_image, $this->fontSize, 0, $this->fontSize*0.7, $this->fontSize*1.7, $this->_color, $this->fontttf, $code);

        // 保存验证码
        $key        =   $this->authcode($this->seKey);
        $secode     =   array();
        $secode['verify_code'] = $sum; // 把校验码保存到session
        $secode['verify_time'] = NOW_TIME;  // 验证码创建时间
        session($key.$id, $secode);

        header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', false);
        header('Pragma: no-cache');
        header("content-type: image/png");

        // 输出图像
        imagepng($this->_image);
        imagedestroy($this->_image);
    }
当然验证的方法也需要修改 /**
     * 加法验证码的验证
     */
    public function add_check($code, $id = '') {
        $key = $this->authcode($this->seKey).$id;
        // 验证码不能为空
        $secode = session($key);
        if(empty($code) || empty($secode)) {
            return false;
        }
        // session 过期
        if(NOW_TIME - $secode['verify_time'] > $this->expire) {
            session($key, null);
            return false;
        }

        if(intval($code) == $secode['verify_code']) {
            $this->reset && session($key, null);
            return true;
        }

        return false;
    }
调用的方法public function test4(){
         $config =    array(
             'fontSize'    =>    16,    // 验证码字体大小
             'length'      =>    4,     // 验证码位数
             'useNoise'    =>    false, // 关闭验证码杂点
             'fontttf'     =>   '2.ttf',// 字体
         );
         $Verify = new \Think\Verify($config);
         $Verify->add_entry();
     }
只尝试了一种思路,不是太完善,轻喷。
广告:承接网站开发,APP开发,公司/个人用cms系统开发。欢迎咨询QQ771831851
博客地址 http://www.zhoukoup.com
评论( 相关
后面还有条评论,点击查看>>