在控制器中:
/** * 输出验证码 */ public function getverify(){ $VModle=new \Think\Verify(); $VModle->codeSet=‘0123456789‘; $VModle->length=4; $VModle->entry(); }
在页面中的html
<input type="text" name="verify" placeholder="验证码" class="form-control" style="width:100px;float: left;"> <img id="verifyimg" src="{:U(‘Public/getverify‘)}" width="100">
javacsript即时刷新
$("#verifyimg").click(function() { var verifyURL = "{:U(‘Public/getverify‘, ‘‘, ‘‘)}"; var time = new Date().getTime(); $(this).attr({"src" : verifyURL+"/"+time}); });
异步验证是否正确:
/** * 异步验证验证码是否正确 * @params verify; */ public function ajaxcheckverify(){ if(!IS_POST){ $data[‘status‘] = 0; } else{ $VModle=new \Think\Verify(); $code = trim(I(‘verify‘)); if(!$VModle->check($code)){ $data[‘status‘] = 0; $data[‘data‘] = $code; } else{//验证成功 $data[‘status‘] = 1; } } echo json_encode($data); }
时间: 2024-10-22 22:10:06