TP5验证码操作

本文与TP5手册略有不同,实现也很简单。

1、首先确认文件夹\vendor\topthink\think-captcha存在


2、显示验证码的方法,我这里是写在:\application\admin\controller\Login.php

  1. //显示验证码  
  2.     public function show_captcha(){  
  3.         $captcha = new \think\captcha\Captcha();  
  4.         $captcha->imageW=121;  
  5.         $captcha->imageH = 32;  //图片高  
  6.         $captcha->fontSize =14;  //字体大小  
  7.         $captcha->length   = 4;  //字符数  
  8.         $captcha->fontttf = '5.ttf';  //字体  
  9.         $captcha->expire = 30;  //有效期  
  10.         $captcha->useNoise = false;  //不添加杂点  
  11.         return $captcha->entry();  
  12.     }  

3、模板文件\application\admin\view\Login\index.html中这样引用验证码
[html] view plain copy
  1. <form action="/login/login_post" method="post">  
  2. <input type="text" class="input" name="captcha" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码"  style="width: 200px;"/>  
  3.                                 <img src="/login/show_captcha" alt="" width="121" height="32" class="passcode" onclick="this.src=this.src+'?'"/>  

4、同控制器中login_post方法

  1. //提交  
  2.     public function login_post(){  
  3.         $code=input('post.captcha');  
  4.         $captcha = new \think\captcha\Captcha();  
  5.         $result=$captcha->check($code);  
  6.         if($result===false){  
  7.             echo '验证码错误';exit;  
  8.         }  
  9.         echo '验证码正确,继续';exit;  
  10.     }  

大笑这样就完成了thinkphp5中的验证码操作


转载请注明出处,转载自:https://blog.csdn.net/leejianjun/article/details/78720698


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值