The example in this article describes the simple way to use the verification code in thinkPHP. Share it with everyone for your reference, the details are as follows:
First generate the verification code. In the action file, directly call the method provided in thinkphp to generate it. Make sure to enable the PHP extension gd2
is as follows:
class UserAction Model extends Model { /** * 显示验证码信息 */ public function verify() { ob_clean(); // 清空(擦掉)输出缓冲区 ,也就是清空前面的输出,通常情况下验证码不显示,可考虑这个问题 import('ORG.Util.Image'); Image::buildImageVerify(); } }
At the same time, use SESSION to save the value of the generated verification code: Copy the code The code is as follows: $_SESSION['verify']
Note: What is saved is the value encrypted with md5.
In the corresponding tpl file, call the verification code. The usage method is as follows:
Copy code The code is as follows:Click to refresh function
The display effect is as follows:
The past verification code submitted by the user needs to be md5 encrypted and then compared with the saved session value , that is:
Judge whether md5($_POST['verify'] and $_SESSION['verify'] are equal.
This completes the basic use of the verification code
I hope this article will be helpful to everyone’s PHP programming based on the thinkPHP framework.