1. 程式人生 > >TP5驗證碼操作

TP5驗證碼操作

本文與TP5手冊略有不同,實現也很簡單。

1、首先確認資料夾\vendor\topthink\think-captcha存在


2、顯示驗證碼的方法,我這裡是寫在:\application\admin\controller\Login.php

  1. //顯示驗證碼
  2.     publicfunction 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中這樣引用驗證碼
  1. <formaction="/login/login_post"method
    ="post">
  2. <inputtype="text"class="input"name="captcha"placeholder="填寫右側的驗證碼"data-validate="required:請填寫右側的驗證碼"style="width: 200px;"/>
  3.                                 <imgsrc="/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