Home > PHP Framework > ThinkPHP > What to do if thinkphp verification code error does not refresh

What to do if thinkphp verification code error does not refresh

PHPz
Release: 2023-04-17 10:14:42
Original
829 people have browsed it

Thinkphp is an open source Web application framework based on the MVC model. It provides many excellent functions and features, allowing developers to develop Web applications more efficiently. One of them is the captcha function. Verification code, the full name of "graphical verification code", is a technical means used to prevent malicious robots from registering or logging in. Typically, when a user enters an incorrect verification code, the website will refresh or regenerate a verification code image. However, some users have encountered the problem of Thinkphp verification code error but not refreshing. What is going on?

1. Problem description

In Thinkphp, the verification code generation and verification uses Thinkphp’s own verification code library. When using this class library, users will find that when the verification code is entered incorrectly, the website will not refresh the verification code immediately. If the user enters the wrong verification code multiple times in a row, the website does not update the verification code, which makes the user feel very inconvenienced.

2. Problem Analysis

The reason for this problem is that in Thinkphp's verification code library, there is a method with the attribute $reset set to false. When the value of this attribute is false, the verification code will not be refreshed until it expires. So when the user enters the wrong verification code multiple times, the website will not update the verification code.

3. Solution

The solution to this problem is also very simple, just change the $reset attribute value to true. The modification method is as follows:

Find the following code in 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);
   }
Copy after login

Modify the $reset attribute value to true. The modified code is as follows :

   //是否画混淆曲线
   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);
   }
Copy after login

After modification, save and resubmit.

4. Conclusion

This article introduces the causes and solutions to the problem of Thinkphp verification code errors not refreshing. This problem can be solved by modifying just one line of code. In fact, when using any framework, it is inevitable that problems will arise. But as long as we actively look for solutions, the problem will always be solved.

The above is the detailed content of What to do if thinkphp verification code error does not refresh. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template