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.验证码的设置: