使用ThinkPHP實現產生/檢驗驗證碼功能

來源:互聯網
上載者:User

標籤:設定   ttf   splay   驗證   函數   收集   資訊   方法   方法校正   

首先瞭解父類Verity.class.php(ThinkPHP/Library/Think/Verity.class.php)中的一些函數

1:check() 校正驗證碼是否正確

2:entry()輸出驗證碼並把驗證碼的值儲存的session中

3:authcode() 加密驗證碼(此方法在check()方法中被調用)

控制器產生驗證碼代碼:

//驗證碼    function verifyImg(){        //驗證碼配置        $cfg = array(           ‘imageH‘ => 30,   // 驗證碼圖片高度           ‘imageW‘ => 100,  // 驗證碼圖片寬度           ‘fontSize‘ => 15, // 驗證碼字型大小(px)           ‘length‘ => 4,    // 驗證碼位元           ‘fontttf‘ => ‘4.ttf‘,  // 驗證碼字型,不設定隨機擷取        );        //執行個體化Verify類 自動調用建構函式,把$cfg資訊傳給$config        $verify = new Verify($cfg);        $verify -> entry();    //輸出驗證碼    }

前台顯示驗證碼

<!--分組/控制器/方法-->
<img src="<{$smarty.const.__CONTROLLER__}>/verifyImg" onclick = "this.src=‘<{$smarty.const.__CONTROLLER__}>/verifyImg/‘+Math.random()" alt="驗證碼" />

控制器校正證碼(以登陸為例)

//登陸    function login(){              //兩個邏輯:展示,收集        if (!empty($_POST)) {            $verify = new Verify();            //接收驗證碼            $captcha = $_POST[‘captcha‘];            //check()方法校正驗證碼            if ($verify->check($captcha)) {                echo "正確";            }else{                echo "錯誤";            }        }       //調用view視圖       $this->display();    }

 

使用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.