一、生成验证码
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Verify { public function index(){ $config = array( 'fontSize' => 100, 'length' => 4, 'useCurve' => false, 'bg' => [191, 250, 222], 'fontttf' => '2.ttf', ); $Verify = new \org\Verify($config); $Verify->entry(); } } |
二、模板中使用验证码
1 2 3 4 5 6 7 8 9 10 11 |
<div class="form-group margin-big-bottom"> <div class="label"> <label for="verify">验证码</label> </div> <div class="field clearfix"> <input type="text" class="input" style="width:60%;float: left;" id="verify" name="verify" placeholder="请填入验证码" tabindex="3"/> <img class="verifyimg reloadverify x4" src="{:U('admin/Verify/index')}" alt="切换验证码" title="切换验证码" style="cursor: pointer;width:100px;height:34px;display:block;float:left;margin-left:15px;vertical-align: middle;"/> </div> </div> |
三、点击切换验证码
1 2 3 4 5 6 7 8 9 10 11 12 |
<script> $(function () { var verifyimg = $(".verifyimg").attr("src"); $(".reloadverify").click(function () { if (verifyimg.indexOf('?') > 0) { $(".verifyimg").attr("src", verifyimg + '&random=' + Math.random()); } else { $(".verifyimg").attr("src", verifyimg.replace(/\?.*$/, '') + '?' + Math.random()); } }); }); </script> |
四、验证验证码是否正确
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if ( !check_verify( $userPost['verify'] ) ) { $this->ajaxTip( '验证码为空或不正确!' ); } /** * 检测验证码 * @param $code * @param int $id * User: Sam:yyzm@vip.qq.com */ function check_verify( $code , $id = '' ) { $verify = new \org\Verify(); return $verify->check( $code , $id ); } |
到此,Thinkphp5验证码功能完成了。
转载请注明:PHP笔记 » Thinkphp5验证码生成实现及验证是否正确