thinkphp5调用验证码并校验是否正确的方法整理
thinkphp5的验证码路径
http://app.thinkphpwu.com/captcha
login页面
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta http-equiv="Cache-Control" content="no-siteapp"/>
<!--[if lt IE 9]>
<script type="text/javascript" src="__STATIC__/hadmin/lib/html5shiv.js"></script>
<script type="text/javascript" src="__STATIC__/hadmin/lib/respond.min.js"></script>
<![endif]-->
<link href="__STATIC__/hadmin/static/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css"/>
<link href="__STATIC__/hadmin/static/h-ui.admin/css/H-ui.login.css" rel="stylesheet" type="text/css"/>
<link href="__STATIC__/hadmin/static/h-ui.admin/css/style.css" rel="stylesheet" type="text/css"/>
<link href="__STATIC__/hadmin/lib/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css"/>
<!--[if IE 6]>
<script type="text/javascript" src="__STATIC__/hadmin/lib/DD_belatedPNG_0.0.8a-min.js"></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>后台登录 - H-ui.admin v3.1</title>
<meta name="keywords" content="奇葩天地网,www.qipa250.com">
<meta name="description" content="奇葩天地网">
</head>
<body>
<input type="hidden" id="TenantId" name="TenantId" value=""/>
<div class="header"></div>
<div class="loginWraper">
<div id="loginform" class="loginBox">
<form class="form form-horizontal" action="{:url('login/check')}" method="post">
<div class="row cl">
<label class="form-label col-xs-3"><i class="Hui-iconfont"></i></label>
<div class="formControls col-xs-8">
<input id="username" name="username" type="text" placeholder="账户" class="input-text size-L">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-3"><i class="Hui-iconfont"></i></label>
<div class="formControls col-xs-8">
<input id="password" name="password" type="password" placeholder="密码" class="input-text size-L">
</div>
</div>
<div class="row cl">
<div class="formControls col-xs-8 col-xs-offset-3">
<input class="input-text size-L" type="text" placeholder="验证码" name="code"
onblur="if(this.value==''){this.value='验证码:'}"
onclick="if(this.value=='验证码:'){this.value='';}" value="验证码:" style="width:150px;">
<img src="/captcha" title="看不清,换一张" onclick="reloadCaptcha(this)"></div>
</div>
<div class="row cl">
<div class="formControls col-xs-8 col-xs-offset-3">
<label for="online">
<input type="checkbox" name="online" id="online" value="">
使我保持登录状态</label>
</div>
</div>
<div class="row cl">
<div class="formControls col-xs-8 col-xs-offset-3">
<button type="submit" class="btn btn-success radius size-L"> 登 录
</button>
<button type="reset" class="btn btn-default radius size-L"> 取 消
</button>
</div>
</div>
</form>
</div>
</div>
<div class="footer">奇葩天地网 by www.qipa250.com v3.1</div>
<script type="text/javascript" src="__STATIC__/hadmin/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="__STATIC__/hadmin/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript">
//点我刷新验证码
function reloadCaptcha(code_img) {
code_img.src = '/captcha?id=' + Math.random();
}
</script>
</body>
</html>
login控制器的check方法
<?php
namespace app\admin\controller;
use think\Controller;
class Login extends Controller
{
public function index()
{
//加载视图文件
return $this->fetch();
}
//验证用户名密码
public function check()
{
//得到提交的表单信息
$data = input('post.');
dump($data);
//验证码判断
if (!captcha_check($data['code'])) {
$this->error('验证码不正确!');
}
}
}
开始验证
输入错误的验证码
报错
输入正确的验证码
没报错