The following is the tutorial column of ThinkPHP to introduce you to the verification code of the ThinkPHP framework. I hope it will be helpful to friends in need!
Think\Verify class can support the generation and verification functions of verification code.
The following is the simplest way to generate a verification code:
$Verify = new \Think\Verify();$Verify->entry();
The above code will generate a default verification code image and output it, as shown below:
The generated verification code information will be saved in the session, and the data included are:
array('verify_code'=>'当前验证码的值','verify_time'=>'验证码生成的时间戳')
If you need to generate multiple verification codes on one page, the entry method needs to pass in the Identified information.
You can set relevant parameters for the generated verification code to achieve different display effects. These parameters include:
Parameter | Description |
---|---|
expire | Validity period of verification code (seconds) |
useImgBg | Whether to use a background image, the default is false |
fontSize | Verification code font size (pixels) The default is 25 |
useCurve | Whether to use the confusion curve the default is true |
useNoise | Whether to add noise points by default is true |
imageW | The verification code width is set to 0 for automatic calculation |
imageH | Set the verification code height to 0 for automatic calculation |
length | Verification code digits |
fontttf | The specified verification code font defaults to random acquisition |
useZh | Whether to use Chinese verification code |
bg | Verification code background color rgb array setting, such as array(243, 251, 254) |
seKey | Encryption key of verification code |
codeSet | Verification code character set 3.2.1 Newly added |
zhSet | Verification code character set (Chinese) 3.2.1 Added |
Default parameter configuration:
Parameter setting uses two methods.
Instantiate the incoming parameters:
or use dynamic settings, such as:
By default, the font of the verification code is randomly used in the font file under the ThinkPHP/Library/Think/Verify/ttfs/
directory. We can specify the font of the verification code. , for example:
$Verify = new \Think\Verify();// 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf$Verify->fontttf = '5.ttf';$Verify->entry();
supports the verification code background image function, which can be set as follows:
$Verify = new \Think\Verify();// 开启验证码背景图片功能 随机使用 ThinkPHP/Library/Think/Verify/bgs 目录下面的图片$Verify->useImgBg = true;$Verify->entry();
If you want to use Chinese verification Code, you can set:
$Verify = new \Think\Verify();// 验证码字体使用 ThinkPHP/Library/Think/Verify/zhttfs/simhei.ttf$Verify->useZh = true;$Verify->entry();
The display effect is as shown:
If it cannot be displayed normally, please confirm your ThinkPHP/Library/Think/Verify/zhttfs There are Chinese font files under the / directory.
If there is no Chinese font file in ThinkPHP/Library/Think/Verify/zhttfs/, you can download or copy the Chinese font file from C:\Windows\Fonts\. Note that the font file extension is .ttf .
You can use the check
method of the Think\Verify class to check whether the input of the verification code is correct. For example, the following is an encapsulated function for verification code detection. :
// 检测输入的验证码是否正确,$code为用户输入的验证码字符串function check_verify($code, $id = ''){$verify = new \Think\Verify();return $verify->check($code, $id); }
Exercise: Take login as an example:
Controller method:
<?php namespace Home\Controller;use Think\Controller;class LoginController extends Controller { function Login() { if(empty($_POST)) { $this->display(); } else { $code = $_POST["yzm"]; $verify = new \Think\Verify(); if($verify->check($code,2)) //code是用户输入的值 ,2是验证码检测标示,必须与生成的验证码标示相同才能验证 { if($_POST["uid"]!="" && $_POST["pwd"]!="") { $model = D("Users"); $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; $attr = $model->field("Pwd")->find($uid); //var_dump($attr); if($pwd==$attr["pwd"]) { session("uid",$uid); // 跳转页面之前将$uid存入session $this->success("登录成功!","Main"); } else { $this->error("登录失败!"); } } else { $this->error("用户名或者密码不能为空!"); } } else { $this->error("验证码不正确!"); } } }//生成验证码的操作方法 function yzm() { $config = array( 'fontSize' => 30, // 验证码字体大小 'length' => 4, // 验证码位数 'useCurve' => true, // 是否画混淆曲线 'useNoise' => true, // 关闭验证码杂点 'expire' => 60, // 验证码有效期(秒) 'useImgBg' => false, // 使用背景图片 'useZh' => true, // 使用中文验证码 'imageW' => 240, // 验证码宽度 'imageH' => 60, // 验证码高度 'fontttf' => 'simhei.ttf', // 验证码字体 ); $Verify = new \Think\Verify($config); $Verify->entry(2);//参数是生成验证码的标示,适用于同一个页面有多个验证码的时候,生成验证码的标示必须与检测验证码的标示相同,否则验证码不正确 }?>
Request Login method:
The above is the detailed content of Detailed explanation of verification code of ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!