tp5 验证码验证 验证码刷新
首先使用Composer
安装think-captcha
扩展包:
composer require topthink/think-captcha
在控制器中使用下面的代码进行验证码生成:
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\captcha\Captcha;
class Code extends Controller
{
/**
* 返回验证码接口
*
* @return \think\Response
*/
public function verify()
{
$captcha = new Captcha();
return $captcha->entry();
}
}
访问http://www.tp.cn/verify即可访问到:效果如下
在模板中就可以使用下面的代码显示验证码图片
<img src="{:url('index/verify')}" id='img' onclick="Code()" alt="captcha">
验证验证码是否正确:
// 检测输入的验证码是否正确,$value为用户输入的验证码字符串
if( !captcha_check($value ))
{
// 验证失败
}
验证码刷新:
//验证码无刷新
function Code() {
document.getElementById("img").src="{{url('Code')}}?"+Math.random();
}
具体可以看官方文档:
https://www.kancloud.cn/manual/thinkphp5_1/354122