配置参数路径:应用根目录\vendor\topthink\think-captcha\src\Captcha.php。
前端:
<input type="text" name="captcha" lay-verify="required" style="width:40%">
<img src="{:captcha_src()}" id="captcha" onclick="resVerify()" alt="验证码" style="width: 55%;height: 50px">
<a href="javascript:resVerify()">看不清换一张</a>
<script>
function resVerify() {
$("#captcha").attr('src', '{:captcha_src()}')
}
</script>
或者:
<div>{:captcha_img()}</div>
后端使用内置验证方法:
if(!captcha_check($captcha)){//$captcha为前端传过来的input验证码值
//验证失败
};
或者:
$captcha=new Captcha();
if (!$captcha->check($posts['captcha'])){
//验证失败
}
手册上提供的方法没看懂:
$this->validate($data,[
'captcha|验证码'=>'require|captcha'
]);
注意: 验证码是跟username和password一起Post过来的,验证码验证完后,验证用户名和密码时,要从数组中删除验证码单元。