thinkphp验证码错误不刷新怎么办

thinkphp是一款基于mvc模式的开源web应用框架,它提供了诸多优秀的功能和特性,让开发者能够更加高效地开发web应用。其中之一便是验证码功能。验证码,全称为“图形验证码”,是一种用于防止恶意机器人注册或登录的技术手段。通常情况下,当用户输入错误的验证码时,网站会刷新或重新生成一张验证码图片。但是有一些用户遇到了thinkphp验证码错误却不刷新的问题,这是怎么回事呢?

一、问题描述

Thinkphp中,验证码的生成和校验使用的是Thinkphp自带的验证码类库。在使用该类库时,用户会发现出现了一种情况,即当验证码输入错误时,网站不会立即刷新验证码。如果用户连续多次输入错误的验证码,网站并没有更新验证码,这让用户感到非常不便。

二、问题分析

该问题的出现原因是因为在Thinkphp的验证码类库中,存在一个属性$reset为false的方法。当该属性值为false时,即不刷新验证码,直至过期为止。所以当用户多次输入错误的验证码时,网站不会更新验证码。

立即学习PHP免费学习笔记(深入)”;

三、解决方法

针对该问题,解决方法也很简单,只需要把$reset属性值修改为true即可。修改方法如下:

在ThinkPHP/Library/Think/Verify.class.php中找到以下代码:

   //是否画混淆曲线    public $useCurve     = true;    //是否添加杂点    public $useNoise     = true;    //验证码图片宽度    public $imageW       = 130;    //验证码图片高度    public $imageH       = 50;    //验证码位数    public $length       = 4;    //验证码字体大小(px)    public $fontSize     = 25;    //是否画颜色背景    public $useZh        = false;    //验证码种子    protected $seed     = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';    //生成验证码    public function entry(){        //验证码字符        $this->code = $this->makeCode();        session($this->seKey,$this->code);//验证码保存到SESSION中        $width       = ($this->length* $this->fontSize*0.9 + $this->fontSize*1.5);        $height      = $this->fontSize*2;        if( $this->useZh ){            $width  = 230;            $height = 50;        }        //创建图像        $this->image = imagecreate($width,$height);        //设置背景        if($this->useZh)            imagecolorallocate($this->image,244, 220, 215);        else{            $this->bkcolor = imagecolorallocate($this->image, 255, 255, 255);            imagefill($this->image,0,0,$this->bkcolor);        }        //混淆曲线        if ($this->useCurve) {            $this->writeCurve();        }        //杂点        if ($this->useNoise) {            $this->writeNoise();        }        //验证码        $this->writeCode();        header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");        header("Content-type: image/png;charset=utf8");        imagepng($this->image);        imagedestroy($this->image);    }

将其中的$reset属性值修改为true,修改后的代码如下:

   //是否画混淆曲线    public $useCurve     = true;    //是否添加杂点    public $useNoise     = true;    //验证码图片宽度    public $imageW       = 130;    //验证码图片高度    public $imageH       = 50;    //验证码位数    public $length       = 4;    //验证码字体大小(px)    public $fontSize     = 25;    //是否画颜色背景    public $useZh        = false;    //验证码种子    protected $seed     = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';    //生成验证码    public function entry(){        //验证码字符        $this->code = $this->makeCode();        session($this->seKey,$this->code);//验证码保存到SESSION中        $width       = ($this->length* $this->fontSize*0.9 + $this->fontSize*1.5);        $height      = $this->fontSize*2;        if( $this->useZh ){            $width  = 230;            $height = 50;        }        //创建图像        $this->image = imagecreate($width,$height);        //设置背景        if($this->useZh)            imagecolorallocate($this->image,244, 220, 215);        else{            $this->bkcolor = imagecolorallocate($this->image, 255, 255, 255);            imagefill($this->image,0,0,$this->bkcolor);        }        //混淆曲线        if ($this->useCurve) {            $this->writeCurve();        }        //杂点        if ($this->useNoise) {            $this->writeNoise();        }        //验证码        $this->writeCode();        // 以下为代码修改        $this->reset = true;        header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");        header("Content-type: image/png;charset=utf8");        imagepng($this->image);        imagedestroy($this->image);    }

修改完后,保存并重新提交即可。

四、结论

本文介绍了Thinkphp验证码错误不刷新的问题出现原因和解决方法。只需修改一行代码,即可解决该问题。实际上,在使用任何框架时,出现问题的情况都是不可避免的。不过只要我们积极地去寻找解决方法,问题总会被解决。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享