zoukankan      html  css  js  c++  java
  • thinkphp验证码使用

    在thinkphp中使用验证码很容易,只要调用thinkphp现有的方法就可以。当然,php的GD库肯定是要开的(就是在php.ini中要加载gd模块)。
    thinkphp 3.1
    
     ---------------------------------------------------------------------------------
    
    首先,在写Action文件,如:IndexAction.class.php.
    <?php  
    class IndexAction extends Action{  
        //显示验证码  
        public function verifyTest() {  
          $this->display();  
        }  
          
        //检验验证码是否正确  
        public function verifyCheck() {  
          //防止页面乱码  
             header('Content-type:text/html;charset=utf-8'); 
            
          if (md5($_POST['verifyTest']) != Session::get('verify')) {  
            echo '验证码错误';  
          }  
          else {  
            echo '验证码正确';  
              }  
        }  
          
        // 生成验证码  
        public function verify() {  
                import("ORG.Util.Image");  
                Image::buildImageVerify();  
        }  
    }  
    ?>
    在对应的模板文件:Tpldefaultindex目录下新建文件verifyTest.html,内容如下:
    <script type='text/javascript'> 
    //重载验证码  
    function freshVerify() {  
      document.getElementByIdx('verifyImg').src='__URL__/verify/'+Math.random();  
    }  
    </script> 
    <form method='post' action='__URL__/verifyCheck'> 
      <input type='text' name='verifyTest'> 
      <img style='cursor:pointer' title='刷新验证码' src='__URL__/verify' id='verifyImg' onClick='freshVerify()'/> 
      <button type='submit'>确定</button> 
    </form> 
    
    thinkphp 3.2
    
     -----------------------------------------------------------------------------
    
    首先,在写Controllers文件,如:IndexController.class.php.
    
    HomeController 是继承 Controller 的父级控制器 也可以直接继承 Controller

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

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

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

    ?>
    <?php namespace HomeController; use ThinkController; 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 ThinkVerify($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; } } } ?> 在对应的模板文件:ViewsIndex目录下新建文件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>
  • 相关阅读:
    Oracle 冷备份详解【实战案例】
    Oracle Procedure记录
    【基础操作】1.表操作
    Oracle的基本语法,存储函数及触发器
    有志者事竟成 — 雷林鹏
    Les13 性能管理
    SharePoint 2010 创建联系人列表和使用联系人列表
    在SharePoint 2010中创建联系人Web数据库网站
    C#中数组如何赋值
    What Can Happen When You Try to Convert a Standard SharePoint 2010 Farm to Enterprise
  • 原文地址:https://www.cnblogs.com/qhorse/p/4724026.html
Copyright © 2011-2022 走看看