驗證碼與分頁是 Web 專案中最基礎也最常用的兩大功能。本文將詳細說明如何在 ThinkPHP 中實作英文/中文驗證碼,以及靈活客製化的分頁顯示,提供可直接複製套用的完整程式碼。
ThinkPHP 內建 ORG.Util.Image 類別提供驗證碼生成功能:
buildImageVerify($length, $mode, $type, $width, $height, $verifyName)GBVerify($length, $type, $width, $height, $fontface, $verifyName)| 參數 | 說明 | 預設值 |
|---|---|---|
| length | 驗證碼長度 | 4 |
| mode | 字元類型:0=字母, 1=數字, 2=大寫, 3=小寫, 4=中文, 5=混合(排除易混淆字元) | 1 |
| type | 圖片格式 | png |
| width/height | 圖片寬高 | 自動/22(英文), 50(中文) |
| fontface | 中文字體檔案(如 simhei.ttf) | 需自行放入類別目錄 |
| verifyName | Session 儲存鍵名 | verify |
<?php
class CommonAction extends Action {
public function verify() {
import('ORG.Util.Image');
// 英文驗證碼範例:5碼混合模式、gif格式、90x30
// Image::buildImageVerify(5, 5, 'gif', 90, 30, 'verify');
// 中文驗證碼(使用預設參數)
Image::GBVerify();
}
}
?>
<form action="" method="post">
驗證碼:<input type="text" name="verify" />
<img src="__APP__/Common/verify" onclick="show(this)" style="cursor:pointer; vertical-align:middle;" /><br />
<input type="submit" value="註冊" />
</form>
<script type="text/javascript">
function show(obj) {
// 點擊圖片更新驗證碼,加入隨機參數防快取
obj.src = "__APP__/Common/verify/random/" + Math.random();
}
</script>
<?php
// 在處理表單的 Action 中
if ($_SESSION['verify'] != md5($_POST['verify'])) {
$this->error('驗證碼錯誤');
}
?>
simhei.ttf 複製到 ORG/Util/ 目錄下。import('ORG.Util.Image');<?php
class UserAction extends Action {
public function index() {
import('ORG.Util.Page'); // 導入分頁類別
$user = M('user');
$count = $user->count(); // 總筆數
$page = new Page($count, 3); // 每頁 3 筆
// 客製化分頁樣式
$page->setConfig('theme', '<div>總共:%totalRow% %header% %nowPage%/%totalPage% 頁 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage% %end%</div>');
$show = $page->show(); // 取得分頁 HTML
$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();
}
}
?>
<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}
<?php
$page->setConfig('prev', '<img id="prev" src="/Public/img/prev.png" />');
$page->setConfig('next', '<img id="next" src="/Public/img/next.png" />');
$page->setConfig('theme', '%upPage% %downPage%'); // 僅顯示上下頁
?>
<?php
protected function _list($map, $model, $limit = 9, $order = 'add_time') {
$p = empty($_GET['p']) ? 1 : (int)$_GET['p'];
$list = $model->where($map)->order($order)->page($p . ',' . $limit)->select();
import('ORG.Util.Page');
$count = $model->where($map)->count();
$Page = new Page($count, $limit);
$Page->rollPage = 3; // 顯示頁碼數量
$Page->setConfig('theme', " %upPage% %linkPage% %downPage% <li style='width:50px'><a href='javascript:void(0)'>%nowPage%/%totalPage% 頁</a></li>");
return array('list' => $list, 'page' => $Page->show());
}
?>
驗證碼與分頁是 ThinkPHP 專案的基石功能。掌握 Image 類別的多種驗證碼模式與 Page 類別的靈活客製化,能大幅提升開發效率。建議將常用分頁邏輯封裝至基礎控制器,實現程式碼複用。