thinkphp3.2 的验证码使用是非常方便的,几乎可以说把代码粘贴进去就能直接使用
首先做一个生成验证码的方法
LogController.class.php中
// 产生验证码
public function makeVerify(){
$Verify = new \Think\Verify();
$Verify->useImgBg = true;
$Verify->secure = true;
$Verify->entry();
}
在模板中调用这个方法,如下
View/Log/index.html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<script src="__COMMONRESOURCE__/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<table>
<form action="__URL__/login" method="post">
<tr>
<td>用户名</td>
<td><input type="text" name="admin"></td>
<td></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
<td></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="verify"></td>
<td id="changeVerify"><img src="__URL__/makeVerify" alt=""></td>
</tr>
<tr>
<td><button>提交</button></td>
<td></td>
<td></td>
</tr>
</form>
</table>
</body>
</html>
直接生成验证码
如图
此时验证码就直接显示出来了,是不是很简单呢.............
好了,今天就到这了...
.
.
.
.
.
等等......怎么验证呢?好吧,当然也很容易啦,tp给我们都整好了
自建一个函数,写到自己的函数库里就能直接用
我的函数放在了Application/Common/Common/Function.php中
// 校验验证码
function check_verify($code, $id = ''){
$verify = new \Think\Verify();
return $verify->check($code, $id);
}
public function login(){
$admin = D('Admin');
$data = array('adname' => I('post.admin'),
'password'=>I('post.password'),
'verify' =>I('post.verify')
);
$re = check_verify($data['verify']);
if(!$re){
$this->error('验证码错误');
}
// 对用户进行登录验证
$adname = $data['adname'];
$password = $data['password'];
$adminArr = $admin->where(array('adname'=>$adname))->find();
$relpassword = passport_decrypt($adminArr['password'],C('PASSWORD_KEY'));
if($relpassword !== $password){
$this->error('密码不正确');
exit;
}
//保存登录信息----关闭浏览器则消除cookie
cookie('adid',passport_encrypt($adminArr['adid'],C('PASSWORD_KEY')));
cookie('adname',passport_encrypt($adname,C('PASSWORD_KEY')));
header('location:'.U('Index/index'));
}
看到了吗?在
$re = check_verify($data['verify']);
直接验证,然后对$re进行判断,验证成功返回true,失败返回false
这样的话,验证码的生成,校验都结束了,
还有一些小问题,当我点击验证码换一张是怎么实现的呢,其实也也很简单啦,验证码就是一张图片,换下图片路径就可以了,用jquery事件,为了防止缓存问题,需要在路径后面拼接上随机数,这样也不会有缓存问题了
--------------------------------------------------------------------------------------------------------------------------------------------------------
完整的前台代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<script src="__COMMONRESOURCE__/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<table>
<form action="__URL__/login" method="post">
<tr>
<td>用户名</td>
<td><input type="text" name="admin"></td>
<td></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
<td></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="verify"></td>
<td id="changeVerify"><img src="__URL__/makeVerify" alt=""></td>
</tr>
<tr>
<td><button>提交</button></td>
<td></td>
<td></td>
</tr>
</form>
</table>
<script>
$('#changeVerify').click(function(){
var num = Math.random();
$(this).find('img').attr('src','__URL__/makeVerify/s/'+num);
});
</script>
</body>
</html>
------------------------------------------------------
完整的后台代码
<?php
namespace Admin\Controller;
use Think\Controller;
class LogController extends Controller {
public function index(){
$adid = cookie('adid');
$adname = cookie('adname');
if(isset($adid) && isset($adname)) header('location:'.U('Index/index'));
$this->display();
}
// 产生验证码
public function makeVerify(){
$Verify = new \Think\Verify();
$Verify->useImgBg = true;
$Verify->secure = true;
$Verify->entry();
}
public function login(){
$admin = D('Admin');
$data = array('adname' => I('post.admin'),
'password'=>I('post.password'),
'verify' =>I('post.verify')
);
$re = check_verify($data['verify']);
if(!$re){
$this->error('验证码错误');
}
// 对用户进行登录验证
$adname = $data['adname'];
$password = $data['password'];
$adminArr = $admin->where(array('adname'=>$adname))->find();
$relpassword = passport_decrypt($adminArr['password'],C('PASSWORD_KEY'));
if($relpassword !== $password){
$this->error('密码不正确');
exit;
}
//保存登录信息----关闭浏览器则消除cookie
cookie('adid',passport_encrypt($adminArr['adid'],C('PASSWORD_KEY')));
cookie('adname',passport_encrypt($adname,C('PASSWORD_KEY')));
header('location:'.U('Index/index'));
}
public function logout(){
cookie('adid',null);
cookie('adname',null);
header('location:'.U('Log/login'));
}
}
注意:不要把自定义校验验证码函数给漏掉了