![](https://img-blog.csdnimg.cn/20210628160729199.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW9kb25nYmFp,size_16,color_FFFFFF,t_70)
//前端
<div class="larry-login" id="captcha-container">
<input name="verify" width="50%" style="height:26px;width:65%" class="captcha-text login_txtbx" placeholder="验证码" type="text" />
<img width="30%" class="left15" height="43" alt="验证码" src="{:U('Login/verify',array())}" title="点击刷新" style="margin-bottom:6px;cursor:pointer">
</div>
//点击图片刷新
<script>
// 验证码生成
var captcha_img = $('#captcha-container').find('img')
var verifyimg = captcha_img.attr("src");
captcha_img.attr('title', '点击刷新');
captcha_img.click(function(){
if( verifyimg.indexOf('?')>0){
$(this).attr("src", verifyimg+'&random='+Math.random());
}else{
$(this).attr("src", verifyimg.replace(/\?.*$/,'')+'?'+Math.random());
}
});
</script>
//控制器中
/*生成验证码*/
public function verify(){
ob_end_clean();//这行要加,不加图片可能不显示
$config =array(
'fontSize' => 15, // 验证码字体大小
'length'=>4, // 验证码位数
'useNoise' => false, // 关闭验证码杂点);
"useCurve"=>false
);
$Verify = new \Think\Verify($config);
$Verify->entry();
}
/* 检查验证码*/
public function check_verify($code, $id = ''){
$verify = new \Think\Verify();
return $verify->check($code, $id);
}
//提交form表单验证
$verify = I("post.verify");
$ver=$this->check_verify($verify);
if(!$ver){
$this->error("验证码输错了哦!");
}