tp5使用驗證碼:
1.html模版調用顯示驗證碼
1 <div class="form-group box-shadow"> 2 <input type="text" class="form-control" id="zhuo_captcha" name="captcha" placeholder="Enter Your captcha "> 3 </div> 4 <img onclick="this.src='{:captcha_src()}?'+Math.random()" src="{:captcha_src()}" alt="captcha" />
效果:
2.PHP控制器,接收驗證碼,並驗證:
if ($this->request->isPost()) { $captcha = new \think\captcha\Captcha(); if(!$captcha->check($this->request->post('captcha'))){ echo json_encode(array("message"=>'提交失敗!','message'=>'驗證碼 錯誤','status'=>0)); die; } } try { $res = \think\Db::name('cms_message')->insert($data); } catch (Exception $e) { $this->error("發生錯誤:" . $e->getMessage()); }
3.驗證碼的設置: