您的位置:首页 > 编程语言 > PHP开发

thinkphp 验证码的使用

2015-08-12 14:35 543 查看
在thinkphp中使用验证码很容易,只要调用thinkphp现有的方法就可以。当然,php的GD库肯定是要开的(就是在php.ini中要加载gd模块)。


thinkphp3.2

-----------------------------------------------------------------------------

首先,在写Controllers文件,如:IndexController.class.php.

HomeController是继承Controller的父级控制器也可以直接继承Controller

在Home文件加下:Home\Common\function.php添加一个检测验证码的封装函数

<?php
    functioncheck_verify($code,$id=""){
      
       $verify=new\Think\Verify();

        return$verify->check($code,$id);
    
    }

?>

<?php
namespaceHome\Controller;
useThink\Controller;
classIndexControllerextendsHomeController{
//显示验证码
publicfunctionindex(){
$this->display();
}

//生成验证码
publicfunctionverify(){
$arr=array(
'imageW'=>130,//验证码显示的款地
'imageH'=>34,//验证码显示的高度
'fontSize'=>18,//验证码字体大小
'length'=>4,//验证码位数
'useNoise'=>false,//关闭验证码杂点true开启
'useCurve'=>false,//关闭验证码曲线true开启
'bg'=>array(228,238,238)//设置背景色
);
$verify=new\Think\Verify($arr);
$verify->entry();
}
//校验验证码
publicfunctionverifyCheck(){
//防止页面乱码
header('Content-type:text/html;charset=utf-8');
$verify=I("post.verify");
$result=check_verify($verify);
if($result){
echo"验证通过!";
exit;
}else{
echo"验证码错误!";
exit;
}
}

}
?>
在对应的模板文件:Views\Index\目录下新建文件index.html,内容如下:
<scripttype='text/javascript'>
//重载验证码
functionfreshVerify(){
document.getElementById('verifyImg').src='{:U("Index/verify")}?'+Math.random();
}
</script>
<formmethod='post'action='{:U("Index/verifyCheck")}'>
<inputtype='text'name='verify'required='required'/>
<imgstyle='cursor:pointer'title='刷新验证码'src='{:U("Index/verify")}'id='verifyImg'onClick='freshVerify()'/>
<buttontype='submit'>确定</button>
</form>





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: