前面提到验证码的使用,但是你会发现点击切换验证码之后,会提示‘验证码错误’;这是因为tp5.0.2自带的验证码类自大的参数$id,默认为空,但是当你点击切换验证码后,$id参数会默认为0,而且会累加 ,所以会永远匹配不上。这里我们也不用去修改框架本身的内容;只需坐这样的修改就可以使用了;
index.php文件
<?php
namespace app\admin\controller;
class Index extends Common {
/*
* 首页
*/
public function index(){
return view();
}
/*
* 登录
*/
public function login(){
if (request()->isPost()) {
$captcha = input('post.code') ? input('post.code') : $this->error('请输入验证码!');
if (!captcha_check($captcha, 'admin')) {
$this->error('验证码错误!');
} else {
$this->success('登录成功', 'index');
}
} else {
return view();
}
}
}
login.html文件
//引用验证码,添加$id 识别admin;
<img id="code_img" align="top" src="{:captcha_src($id = 'admin')}">
<script>
//jquery,js点击切换验证码
$(function() {
$('#code_img').click(function() {
var imgUrl = $(this).attr('src');
imgUrl = imgUrl.substr(imgUrl.indexOf('/captcha', imgUrl), 14) + '/' +Math.random()+'.html';
$(this).attr('src', imgUrl);
});
})
</script>
这样,验证码类已经完美使用