Home  >  Article  >  Backend Development  >  ThinkPHP验证码和分页实例教程_PHP

ThinkPHP验证码和分页实例教程_PHP

WBOY
WBOYOriginal
2016-05-31 19:30:27770browse

ThinkPHP

本文实例讲述了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页面代码如下:

<&#63;php
class CommonAction extends Action{
  function verify(){    
    import('ORG.Util.Image');
    //英文验证码
    //Image::buildImageVerify(5,5,gif,90,30,'verify');
    //中文验证码
    Image::GBVerify();
  }  
  
}
&#63;>

5.view模板部分:

模板index.html页面如下:

 验证码:<input type="text" name="verify" /><img  src="__APP__/common/verify" onclick="show(this)" / alt="ThinkPHP验证码和分页实例教程_PHP" ><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验证码与分页实例,相信会有新的收获。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn