ThinkPHP验证码和分页实例教程_php实例
本文实例讲述了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页面代码如下:
<?php class 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="/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>
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验证码与分页实例,相信会有新的收获。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What should I do if Google Chrome does not display the verification code image? Sometimes you need a verification code to log in to a web page using Google Chrome. Some users find that Google Chrome cannot display the content of the image properly when using image verification codes. What should be done? The editor below will introduce how to deal with the Google Chrome verification code not being displayed. I hope it will be helpful to everyone! Method introduction: 1. Enter the software, click the "More" button in the upper right corner, and select "Settings" in the option list below to enter. 2. After entering the new interface, click the "Privacy Settings and Security" option on the left. 3. Then click "Website Settings" on the right

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

The virtual number can receive the verification code. As long as the mobile phone number filled in during registration complies with the regulations and the mobile phone number can be connected normally, you can receive the SMS verification code. However, you need to be careful when using virtual mobile phone numbers. Some websites do not support virtual mobile phone number registration, so you need to choose a regular virtual mobile phone number service provider.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.
