PHP

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

字号+ 编辑: 国内TP粉 修订: H波 来源: ThinkPHP 2023-09-08 我要说两句(0)

加法验证码实现

改写验证码类,实现加法验证码
使用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();
     }


只尝试了一种思路,不是太完善。

阅完此文,您的感想如何?
  • 有用

    2

  • 没用

    3

  • 开心

    3

  • 愤怒

    3

  • 可怜

    2

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评