本文与TP5手册略有不同,实现也很简单。
1、首先确认文件夹\vendor\topthink\think-captcha存在
2、显示验证码的方法,我这里是写在:\application\admin\controller\Login.php
- //显示验证码
- public function show_captcha(){
- $captcha = new \think\captcha\Captcha();
- $captcha->imageW=121;
- $captcha->imageH = 32; //图片高
- $captcha->fontSize =14; //字体大小
- $captcha->length = 4; //字符数
- $captcha->fontttf = '5.ttf'; //字体
- $captcha->expire = 30; //有效期
- $captcha->useNoise = false; //不添加杂点
- return $captcha->entry();
- }
3、模板文件\application\admin\view\Login\index.html中这样引用验证码
- <form action="/login/login_post" method="post">
- <input type="text" class="input" name="captcha" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码" style="width: 200px;"/>
- <img src="/login/show_captcha" alt="" width="121" height="32" class="passcode" onclick="this.src=this.src+'?'"/>
4、同控制器中login_post方法
- //提交
- public function login_post(){
- $code=input('post.captcha');
- $captcha = new \think\captcha\Captcha();
- $result=$captcha->check($code);
- if($result===false){
- echo '验证码错误';exit;
- }
- echo '验证码正确,继续';exit;
- }
这样就完成了thinkphp5中的验证码操作
转载请注明出处,转载自:https://blog.csdn.net/leejianjun/article/details/78720698