思路与文件上传相同,只是验证码一个方法:
<img src="{:captcha_src()}" />
后台文件:appceshiyam
<?php namespace appceshicontroller; use thinkController; use thinkDb; class Yzm extends Controller{ public function index() { return $this->fetch(); } public function check($code='') { $captcha = new hinkcaptchaCaptcha(); if(!$captcha->check($code)) { $this->error('验证码错误'); } else { $this->success('验证码正确'); } } }
前端文件:yzm/index.html
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>图片验证示例</title> </head> <body> <h2>图片验证示例</h2> <div> <!--验证数据--> <FORM method="post" class="form" action="{:url('check')}" name='tj'> <p><input name="code" type="text" placeholder="请输入验证码" style="100px;"> <span> <!--{:captcha_img()}--> <img id="verify_img" src="{:captcha_src()}" alt="请点击刷新验证码" onclick="this.src='{:captcha_src()}'+'?'+Math.random()"/> </span> </p> <p style="margin-top:30px;"><input name="tj" type="submit" class="button" value="提交"></p> </FORM> </div> </body> </html>
实现效果:
输入验证码正确:
输入验证码错误: