Home Backend Development PHP Tutorial ThinkPHP验证码和分页实例教程_php实例

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

May 16, 2016 pm 08:37 PM
thinkphp Pagination Verification code

本文实例讲述了ThinkPHP常用的两个功能:验证码与分页。在ThinkPHP的项目开发中非常常见,具有很高的实用价值。完整实例分享给大家,供大家参考。具体如下:

一、验证码:

导入验证码类,在aoli\ThinkPHP\Lib\ORG\Util\Image.class.php里有验证码方法   

1.英文验证码:

buildImageVerify($length,$mode,$type,$width,$height,$verifyName)
Copy after login

参数如下:

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)
Copy after login

参数如下:

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;>

Copy after login

5.view模板部分:

模板index.html页面如下:

 验证码:<input type="text" name="verify" /><img  src="/static/imghw/default1.png"  data-src="__APP__/common/verify"  class="lazy" 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>

Copy after login

6.控制器:

控制器UserAction.class.php如下:

//验证码验证
if($_SESSION['verify']!=md5($_POST['verify'])){
  $this->error('验证码不正确');   
}

Copy after login

二、分页:

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();
}

Copy after login

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}

Copy after login

感兴趣的读者可以调试运行一下本文ThinkPHP验证码与分页实例,相信会有新的收获。

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? Mar 13, 2024 pm 08:55 PM

What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code?

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

How to run thinkphp project

Can virtual numbers receive verification codes? Can virtual numbers receive verification codes? Jan 02, 2024 am 10:22 AM

Can virtual numbers receive verification codes?

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

There are several versions of thinkphp

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

How to run thinkphp

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

How to install thinkphp

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Which one is better, laravel or thinkphp?

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks

See all articles