PHP--TP架構----產生驗證碼的方式

來源:互聯網
上載者:User

標籤:

                                                     TP架構----產生驗證碼的方式

xianshi.html

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title></head><body><img src="__CONTROLLER__/YZM"><!--src=""指向的是操作方法。"__CONTROLLER__/YZM"當前控制器下--></body></html>

 

  

 

 

1、

 

                                          【2】                                             【1】

【1】把驗證碼的類造一個對象出來。

【2】調用對象裡的方法,方法是entry()----可以產生驗證碼。

 驗證碼的類----Verify.class.php

<?phpnamespace Home\Controller;//這個檔案的命名空間use Think\Controller;//use使用哪一個而命名空進。找Controller父類的檔案//http://localhost/ThinkPHP/index.php/Home/Diyi/Testclass DiyiController extends Controller {       //驗證碼  function YZM() //YZM--操作方法  {  //第一步,造驗證碼的對象  $v = new \Think\Verify();//通過命名空間來找到類。在初始命名空間下的Think,下面有Verify.class.php  //調用對象裡的方法  $v->entry();//調用這個entry()方法就可產生驗證碼  }    function xianshi()  { //這個方法在執行是,把頁面列印出來 $this->display();   }      }  

顯示的結果:

 

2、

這個頁面需要產生多個驗證碼-----如何進行區分??

                  entry()方法裡可以寫參數,寫參數代表的是給驗證碼進行區分,區分是第幾個

 

 3、

例子:

<?phpnamespace Home\Controller;//這個檔案的命名空間use Think\Controller;//use使用哪一個而命名空進。找Controller父類的檔案//http://localhost/ThinkPHP/index.php/Home/Diyi/Testclass DiyiController extends Controller {      //驗證碼  function YZM() //YZM--操作方法  {  $config =  array(            ‘fontSize‘    =>   30,          ‘length‘      =>    3,                 );  $v =  new \Think\Verify($config);  $v ->entry();      //第一步,造驗證碼的對象  $v = new \Think\Verify();//通過命名空間來找到類。在初始命名空間下的Think,下面有Verify.class.php  //調用對象裡的方法  $v->entry();//調用這個entry()方法就可產生驗證碼  }    function xianshi()  { //這個方法在執行是,把頁面列印出來 $this->display();   }      }  

  

                                                        給驗證碼加參數

顯示的結果:

 

 4、驗證碼的字型

ttfs要有相應的檔案:

                     【2】                          【1】

 【1】英文的字型檔

 【2】中文的字型檔

                              這個屬性,可以設定要使用哪一個字型 

 

 5、驗證碼的背景圖片

 

                       useImgBg 預設是false,要改成true。改完之後,隨機使用 ThinkPHP/Library/Think/Verify/bgs 目錄下面的圖片。

 

 

             驗證碼的背景圖片檔案

例子:

<?phpnamespace Home\Controller;//這個檔案的命名空間use Think\Controller;//use使用哪一個而命名空進。找Controller父類的檔案//http://localhost/ThinkPHP/index.php/Home/Diyi/Testclass DiyiController extends Controller {      //驗證碼  function YZM() //YZM--操作方法  {  $config =  array(            ‘fontSize‘    =>   30,          ‘length‘      =>    3, ‘useImgBg‘ => true,                 );  $v =  new \Think\Verify($config);  $v ->entry();      //第一步,造驗證碼的對象  $v = new \Think\Verify();//通過命名空間來找到類。在初始命名空間下的Think,下面有Verify.class.php  //調用對象裡的方法  $v->entry();//調用這個entry()方法就可產生驗證碼  }    function xianshi()  { //這個方法在執行是,把頁面列印出來 $this->display();   }      }  

                                             換驗證碼的背景圖片

顯示的結果:

第一次,重新整理:

第二次,重新整理:

 

 6、中文驗證碼

 

找字型檔:

                                     必須是ttf格式

把上面的檔案複製到

<?phpnamespace Home\Controller;//這個檔案的命名空間use Think\Controller;//use使用哪一個而命名空進。找Controller父類的檔案//http://localhost/ThinkPHP/index.php/Home/Diyi/Testclass DiyiController extends Controller {         //驗證碼  function YZM() //YZM--操作方法  {  $config =  array(            ‘fontSize‘    =>   30,          ‘length‘      =>    3, //‘useImgBg‘ => true, ‘useZh‘ => true,‘fontttf‘ => ‘simkai.ttf‘,                );  $v =  new \Think\Verify($config);  $v ->entry();      //第一步,造驗證碼的對象  $v = new \Think\Verify();//通過命名空間來找到類。在初始命名空間下的Think,下面有Verify.class.php  //調用對象裡的方法  $v->entry();//調用這個entry()方法就可產生驗證碼  }    function xianshi()  { //這個方法在執行是,把頁面列印出來 $this->display();   }      }  

                                        fontttf=‘‘表示:所示用哪一個字型。   fontttf =‘要寫字型的檔案名稱’

  

 顯示的 結果:

如果只設定‘useZh‘ => true,找不到字型,因為預設使用的是英文的

 

PHP--TP架構----產生驗證碼的方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.