ThinkPHP驗證碼和分頁執行個體教程_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了ThinkPHP常用的兩個功能:驗證碼與分頁。在ThinkPHP的項目開發中非常常見,具有很高的實用價值。完整執行個體分享給大家,供大家參考。具體如下:

一、驗證碼:

匯入驗證碼類,在aoli\ThinkPHP\Lib\ORG\Util\Image.class.php裡有驗證碼方法   

1.英文驗證碼:

buildImageVerify($length,$mode,$type,$width,$height,$verifyName)

參數如下:

length :驗證碼的長度,預設為 4 位元
mode :驗證字串的類型,預設為數字,其他支援類型有 0 字母 1 數字 2 大寫字母 3 小寫字母 4
中文 5 混合(去掉了容易混淆的字元 oOLl 和數字 01 )
type :驗證碼的圖片類型,預設為 png
width :驗證碼的寬度,預設會自動根據驗證碼長度自動計算
height :驗證碼的高度,預設為 22
verifyName :驗證碼的 SESSION 記錄名稱,預設為 verify

2.中文驗證碼:

GBVerify ($length,$type,$width,$height,$fontface,$verifyName)

參數如下:

length :驗證碼的長度,預設為 4 位元
type :驗證碼的圖片類型,預設為 png
width :驗證碼的寬度,預設會自動根據驗證碼長度自動計算
height :驗證碼的高度,預設為 50
fontface :使用的字型檔,使用完整檔案名稱或者放到映像類所在的目錄下面,預設使用的字型檔是 simhei.ttf (該檔案可以從 window 的 Fonts 目錄下面找到)
verifyName :驗證碼的 SESSION 記錄名稱,預設為 verify

3.如果無法顯示驗證碼,請檢查:

①.PHP 是否已經安裝 GD 庫支援; 
②.輸出之前是否有任何的輸出(尤其是 UTF8 的 BOM 頭資訊輸出);
③.Image 類庫是否正確匯入;
④.如果是中文驗證碼檢查是否有拷貝字型檔到類庫所在目錄;

4.action部分:

CommonAction.class.php頁面代碼如下:

<?phpclass CommonAction extends Action{  function verify(){        import('ORG.Util.Image');    //英文驗證碼    //Image::buildImageVerify(5,5,gif,90,30,'verify');    //中文驗證碼    Image::GBVerify();  }    }?>

5.view模板部分:

模板index.html頁面如下:

 驗證碼:<input type="text" name="verify" /><img src="__APP__/common/verify" onclick="show(this)" /><br /> <input type="submit" value="註冊" /></form><script type="text/javascript">  function show(obj){    obj.src="__APP__/common/verify/random/"+Math.random();      }</script>

6.控制器:

控制器UserAction.class.php如下:

//驗證碼驗證if($_SESSION['verify']!=md5($_POST['verify'])){  $this->error('驗證碼不正確');   }

二、分頁:

1.匯入分頁類,在aoli\ThinkPHP\Lib\ORG\Util\Page.class.php裡有驗證碼方法  

2.action部分:

UserAction.class.php頁面如下:

function index(){  import('ORG.Util.Page');//引入分布類  $user=M('user');  $count=$user->count();  $page=new Page($count,3);//一頁顯示5條  $page->setConfig('theme','<div style="font-weight:bold;">總共:%totalRow%%header% %nowPage%/%totalPage%頁 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage% %end%</div>');  $show=$page->show();  $list=$user->field(array('id','username','createip'))->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();  $this->assign('alist',$list);  $this->assign('page',$show);  $this->display();}

3.view模板部分:

模板頁index.html頁面如下:

<volist name="alist" id="vo"> <li><span>ID:</span>{$vo['id']}<span>使用者名稱:</span>{$vo['username']}<span>註冊ip:</span>{$vo['createip']}<a href="__URL__/del/id/{$vo['id']}">刪除</a>  <a href="__URL__/edit/id/{$vo['id']}">編輯</a></li></volist>{$page}

感興趣的讀者可以調試運行一下本文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.