blts

thinkphp 验证码的使用

在thinkphp中使用验证码很容易,只要调用thinkphp现有的方法就可以。当然,php的GD库肯定是要开的(就是在php.ini中要加载gd模块)。
thinkphp 3.2

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

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

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

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

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

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

?>

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends HomeController {
    //显示验证码
    public function index() {
        $this->display();
    }
      
    // 生成验证码  
    public function verify() {  
        $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(); 
    } 
    //校验验证码
    public function verifyCheck() {
        //防止页面乱码  
           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,内容如下:
<script type=\'text/javascript\'> 
    //重载验证码  
    function freshVerify() {  
          document.getElementById(\'verifyImg\').src=\'{:U("Index/verify")}?\'+Math.random();  
    }  
    </script> 
    <form method=\'post\' action=\'{:U("Index/verifyCheck")}\'> 
        <input type=\'text\' name=\'verify\' required=\'required\' /> 
        <img style=\'cursor:pointer\' title=\'刷新验证码\' src=\'{:U("Index/verify")}\' id=\'verifyImg\' onClick=\'freshVerify()\'/> 
        <button type=\'submit\'>确定</button> 
    </form>

 

 
发表于 2015-08-12 14:35  百里屠苏phper  阅读(224)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2021-12-04
  • 2021-04-09
  • 2021-12-04
  • 2021-12-04
  • 2021-12-04
  • 2021-12-04
猜你喜欢
  • 2021-11-23
  • 2021-11-21
  • 2022-02-09
  • 2021-10-14
  • 2022-12-23
  • 2021-12-04
  • 2021-12-04
相关资源
相似解决方案