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; ???//驗證碼位數(shù) ???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; ???????} ???????//創(chuàng)建圖像 ???????$this->image?=?imagecreate($width,$height); ???????//設(shè)置背景 ???????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); ???}
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; ???//驗證碼位數(shù) ???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; ???????} ???????//創(chuàng)建圖像 ???????$this->image?=?imagecreate($width,$height); ???????//設(shè)置背景 ???????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); ???}
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)