1.确定项目目录》vendor》topthink》think-captcha目录存在

2.在config中添加验证码配置

//验证码配置
'captcha' => [
// 验证码字符集合
'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
// 验证码字体大小(px)
'fontSize' => 20,
// 是否画混淆曲线
'useCurve' => true,
// 验证码图片高度
'imageH' => 42,
//是否添加杂点
'useNoise'=>true,
// 验证码图片宽度
'imageW' => 148,
// 验证码位数
'length' => 4,
// 验证成功后是否重置
'reset' => true
],

3.模板captcha.html里输出验证码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>验证码</title>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head> <body>
输入验证码: <div>
<img id="verify_img" src="{:captcha_src()}" alt="验证码" onclick="refreshVerify()">
<a href="javascript:refreshVerify()">点击刷新</a>
</div>
<form class="layui-form" action="" >
<input type="text" name = "verify"> <button class="layui-btn" lay-filter="checkcaptcha" lay-submit="" id="checkcaptcha" >
保存
</button>
</form>
<script>
function refreshVerify() {
var ts = Date.parse(new Date())/1000;
var img = document.getElementById('verify_img');
img.src = "{:captcha_src()}";
}
</script>
<script>
$(function () {
$("#checkcaptcha").on("click",function(){
$.ajax({
type: 'POST',
url: "{:url('test/checkcaptcha')}",
data: $(".layui-form").serialize(),
dataType: "json",
async: false,
error: function(request) {
alert("发送请求失败!");
},
success: function(data){
console.log(data);
if (data.status == 1) {
alert(data.message); } else {
alert(data.message);
}
} });
})
})
</script> </body>
</html>

4.在控制器Test.php中书写验证码检验逻辑

/**
* 验证码1-1
*
* @return \think\Response
*/
public function captcha()
{
return $this -> view -> fetch('captcha');
}
/**
* 验证码1-2
*
* @return \think\Response
*/
public function checkcaptcha()
{
$status=1;
$captcha = input('verify');
if(!captcha_check($captcha)){
//验证码错误
$message='验证码错误';

}else{
//验证码正确
$message='验证码正确';
}
return ['status'=> $status, 'message'=> $message];
}

最新文章

  1. jira的插件开发流程实践
  2. setAlpha方法 设置透明度
  3. 2014 Super Training #6 A Alice and Bob --SG函数
  4. G-sensor 与M-sensor区别
  5. 1105. Spiral Matrix (25)
  6. java中的快捷键
  7. 安装交叉编译arm-linux-gcc环境
  8. 第一次写博客,关于前端开发deMVC在js中的应用
  9. POJ 3982 序列 塔尔苏斯问题解决
  10. Sql Server实现多行数据按分组用逗号分隔成一行数据
  11. Redis的二八定律
  12. SVN仓库迁移到Git的完美解决办法
  13. Oracle第一波
  14. Yii2整合AdminLTE后台主题
  15. FastDFS整合nginx后,nginx一直报错
  16. MySql连接时出现1251 client does no support authentic错误解决方法
  17. 常用Gene ID转换工具--转载
  18. struts2学习(1)struts2 helloWorld
  19. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)
  20. android怎样写一个自己定义的dialog能够在Title的位置弹出来

热门文章

  1. AutoIT: 处理GridView控件的一些折中方法
  2. Java Socket实战之三:传输对象
  3. E20180306-hm-xa
  4. bzoj 1230: [Usaco2008 Nov]lites 开关灯【线段树】
  5. RT-Thread 设备驱动ADC浅析与改进
  6. centos 允许远程连接mysql
  7. Poj 3436 ACM Computer Factory (最大流)
  8. 数论 HDOJ 5407 CRB and Candies
  9. 链表中用标兵结点简化代码 分类: c/c++ 2014-09-29 23:10 475人阅读 评论(0) 收藏
  10. 第二个Struts2程序 应用动态Action