1. 验证码的实现
来自公共文件 common.php
/**
* [cx_validatecode_img 生成验证码]
* @param string $imgparam [description]
* @param string $imgattrs [description]
* @return [type] [description]
*/
function cx_validatecode_img($imgparam='',$imgattrs='title="点击获取"'){
$src= "/login/checkcode/".$imgparam;
$img=<<<hello
<img src="$src" onclick="this.src='$src&time='+Math.random();" $imgattrs/>
hello;
return $img;
}
PHP代码 来自Login控制器
/**
* [checkcode 登陆验证码操作方法]
* @return [type] [description]
*/
public function checkcode(){
$captcha = new \think\captcha\Captcha();
$captcha->imageW=121;
$captcha->imageH = 32; //图片高
$captcha->fontSize =14; //字体大小
$captcha->length = 4; //字符数
// $captcha->fontttf = '5.ttf'; //字体
$captcha->expire = 30; //有效期
$captcha->useNoise = false; //不添加杂点
$captcha->reset = true; //验证成功后是否重置
return $captcha->entry();
}
HTML 来自login.html
<tr>
<td>验证码:</td>
<td>{:cx_validatecode_img()}</td>
<td><input type="text" name="validate" placeholder="请输入验证码"></td>
</tr>
2.验证码的验证
// 验证验证码
$validatecode = 'zxu8';
if(!$validatecaptcha = captcha_check($validatecode)){
dump('验证码失败 validatecode false');
die;
}