在控制器中:
1
2
3
4
5
6
7
8
9
|
/**
* 输出验证码
*/
public function getverify(){
$VModle=new \Think\Verify();
$VModle->codeSet='0123456789';
$VModle->length=4;
$VModle->entry();
}
|
在页面中的html
1
2
|
<input type="text" name="verify" placeholder="验证码" class="form-control" style="width:100px;float: left;">
<img id="verifyimg" src="{:U('Public/getverify')}" width="100">
|
javacsript即时刷新
1
2
3
4
5
|
$("#verifyimg").click(function() {
var verifyURL = "{:U('Public/getverify', '', '')}";
var time = new Date().getTime();
$(this).attr({"src" : verifyURL+"/"+time});
});
|
异步验证是否正确:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* 异步验证验证码是否正确
* @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);
}
|