thinkphp驗證碼的實現

來源:互聯網
上載者:User

標籤:

兩種驗證碼驗證實現,一種直接在form表單提交按鈕實現驗證,一種使用ajax傳遞參數實現驗證:

1、直接在form表單提交按鈕實現驗證,在控制器VerifyController.class.php中寫入如下代碼:

namespace Home\Controller;use Think\Controller;class VerifyController extends Controller {    public function index() {        $this->display();    }    public function checkLogin() {        $verify=new \Think\Verify();        $code=I(‘post.verify‘);//表單驗證碼        if($verify->check($code)){            $this->success(‘驗證碼正確‘);        }else{            $this->error(‘驗證碼錯誤‘);        }    }    public function verify()    {        // 執行個體化Verify對象        $verify = new \Think\Verify();        // 配置驗證碼參數        $verify->fontSize = 14;     // 驗證碼字型大小        $verify->length = 4;        // 驗證碼位元        $verify->imageH = 34;       // 驗證碼高度        $verify->useImgBg = true;   // 開啟驗證碼背景        $verify->useNoise = false;  // 關閉驗證碼幹擾雜點        $verify->entry();    }}

在視圖Verify/index.html中的代碼如下:

<!DOCTYPE html><html>    <head>        <title></title>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    </head>    <body>        <form action="{:U(‘verify/checkLogin‘)}" method="post">            <div class="form-group has-feedback">                <input type="text" name="verify" id="verify" placeholder="驗證碼" style="width:100px;" />                <span style="right:120px;"></span>                <img class="verify" src="{:U(verify)}" alt="驗證碼" onClick="this.src=this.src+‘?‘+Math.random()" />            </div>                <div class="col-xs-4">                    <button type="submit" >驗證</button>                </div>        </form>    </body></html>

2、使用ajax傳遞參數實現驗證,在控制器VerifyController.class.php中的代碼如下:

namespace Home\Controller;use Think\Controller;class VerifyController extends Controller {    public function index() {        $this->display();    }    public function checkLogin() {        $verify=new \Think\Verify();        $code=$_POST[‘code‘];//ajax驗證碼擷取        if($verify->check($code)){            $this->ajaxReturn(1);        }else{            $this->ajaxReturn(0);        }    }    public function verify()    {        // 執行個體化Verify對象        $verify = new \Think\Verify();        // 配置驗證碼參數        $verify->fontSize = 14;     // 驗證碼字型大小        $verify->length = 4;        // 驗證碼位元        $verify->imageH = 34;       // 驗證碼高度        $verify->useImgBg = true;   // 開啟驗證碼背景        $verify->useNoise = false;  // 關閉驗證碼幹擾雜點        $verify->entry();    }}

視圖Verify/index.html中的代碼如下:

<!DOCTYPE html><html>    <head>        <title></title>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <script src="__JS__/jquery-2.1.0.min.js" ></script>    </head>    <body>        <form action="{:U(‘verify/checkLogin‘)}" method="post">            <div class="form-group has-feedback">                <input type="text" name="verify" id="verify" placeholder="驗證碼" style="width:100px;" />                <span style="right:120px;"></span>                <img class="verify" src="{:U(verify)}" alt="驗證碼" onClick="this.src=this.src+‘?‘+Math.random()" />            </div>                <div class="col-xs-4">                    <button type="button" id="ver">驗證</button>                </div>        </form>        <script>            $(document).ready(function(){                /*ajax驗證碼*/                $("#ver").click(function(){                   var code=$("#verify").val();//擷取輸入驗證碼                   var url=$(‘form‘).attr(‘action‘);//擷取表單action的值                   $.ajax({                       type:"post",                       url:url,                       data:{"code":code},                       error:function(request){                           alert("ajax錯誤");                       },                       success:function(data){                           if(data){                               alert("正確")                           }else{                               alert(‘錯誤‘)                           }                       }                   });                });            });        </script>    </body></html>

在第2種方法,不要忘記下載jquery.min.js檔案:http://www.jq22.com/jquery-info122

在設定檔Common/conf/config.php中配置地址:

return array(      /*地址替換*/    ‘TMPL_PARSE_STRING‘=>array(          ‘__JS__‘=>__ROOT__.‘/Public/JS‘,    ),);

 

thinkphp驗證碼的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.