TP5驗證碼操作
阿新 • • 發佈:2019-02-03
本文與TP5手冊略有不同,實現也很簡單。
1、首先確認資料夾\vendor\topthink\think-captcha存在
2、顯示驗證碼的方法,我這裡是寫在:\application\admin\controller\Login.php
- //顯示驗證碼
- publicfunction 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中這樣引用驗證碼
- <formaction="/login/login_post"method
- <inputtype="text"class="input"name="captcha"placeholder="填寫右側的驗證碼"data-validate="required:請填寫右側的驗證碼"style="width: 200px;"/>
- <imgsrc="/login/show_captcha"alt=""width="121"height="32"class="passcode"onclick="this.src=this.src+'?'"/>
4、同控制器中login_post方法
- //提交
- public
- $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