PHP验证码类代码( 最新修改,完全定制化! )


Posted in PHP onDecember 02, 2010

Authnum.class.php 下载

<?php 
session_start(); 
class Authnum { 
//图片对象、宽度、高度、验证码长度 
private $im; 
private $im_width; 
private $im_height; 
private $len; 
//随机字符串、y轴坐标值、随机颜色 
private $randnum; 
private $y; 
private $randcolor; 
//背景色的红绿蓝,默认是浅灰色 
public $red=238; 
public $green=238; 
public $blue=238; 
/** 
* 可选设置:验证码类型、干扰点、干扰线、Y轴随机 
* 设为 false 表示不启用 
**/ 
//默认是大小写数字混合型,1 2 3 分别表示 小写、大写、数字型 
public $ext_num_type=''; 
public $ext_pixel = false; //干扰点 
public $ext_line = false; //干扰线 
public $ext_rand_y= true; //Y轴随机 
function __construct ($len=4,$im_width='',$im_height=25) { 
// 验证码长度、图片宽度、高度是实例化类时必需的数据 
$this->len = $len; $im_width = $len * 15; 
$this->im_width = $im_width; 
$this->im_height= $im_height; 
$this->im = imagecreate($im_width,$im_height); 
} 
// 设置图片背景颜色,默认是浅灰色背景 
function set_bgcolor () { 
imagecolorallocate($this->im,$this->red,$this->green,$this->blue); 
} 
// 获得任意位数的随机码 
function get_randnum () { 
$an1 = 'abcdefghijklmnopqrstuvwxyz'; 
$an2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
$an3 = '0123456789'; 
if ($this->ext_num_type == '') $str = $an1.$an2.$an3; 
if ($this->ext_num_type == 1) $str = $an1; 
if ($this->ext_num_type == 2) $str = $an2; 
if ($this->ext_num_type == 3) $str = $an3; 
for ($i = 0; $i < $this->len; $i++) { 
$start = rand(1,strlen($str) - 1); 
$randnum .= substr($str,$start,1); 
} 
$this->randnum = $randnum; 
$_SESSION[an] = $this->randnum; 
} 
// 获得验证码图片Y轴 
function get_y () { 
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5); 
else $this->y = $this->im_height / 4 ; 
} 
// 获得随机色 
function get_randcolor () { 
$this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200)); 
} 
// 添加干扰点 
function set_ext_pixel () { 
if ($this->ext_pixel) { 
for($i = 0; $i < 100; $i++){ 
$this->get_randcolor(); 
imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor); 
} 
} 
} 
// 添加干扰线 
function set_ext_line () { 
if ($this->ext_line) { 
for($j = 0; $j < 2; $j++){ 
$rand_x = rand(2, $this->im_width); 
$rand_y = rand(2, $this->im_height); 
$rand_x2 = rand(2, $this->im_width); 
$rand_y2 = rand(2, $this->im_height); 
$this->get_randcolor(); 
imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor); 
} 
} 
} 
/**创建验证码图像: 
* 建立画布(__construct函数) 
* 设置画布背景($this->set_bgcolor();) 
* 获取随机字符串($this->get_randnum ();) 
* 文字写到图片上(imagestring函数) 
* 添加干扰点/线($this->set_ext_line(); $this->set_ext_pixel();) 
* 输出图片 
**/ 
function create () { 
$this->set_bgcolor(); 
$this->get_randnum (); 
for($i = 0; $i < $this->len; $i++){ 
$font = rand(4,6); 
$x = $i/$this->len * $this->im_width + rand(1, $this->len); 
$this->get_y(); 
$this->get_randcolor(); 
imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor); 
} 
$this->set_ext_line(); 
$this->set_ext_pixel(); 
header("content-type:image/png"); 
imagepng($this->im); 
imagedestroy($this->im); //释放图像资源 
} 
}//end class 
/**使用验证码类的方法: 
* $an = new Authnum(验证码长度,图片宽度,图片高度); 
* 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片 
* 表单页面检测验证码的方法,对比 $_SESSION[an] 是否等于 $_POST[验证码文本框ID] 
* 可选配置: 
* 1.验证码类型:$an->ext_num_type=1; 值为1是小写类型,2是大写类型,3是数字类型 
* 2.干扰点:$an->ext_pixel = false; 值为false表示不添加干扰点 
* 3.干扰线:$an->ext_line = false; 值为false表示不添加干扰线 
* 4.Y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片Y轴随机 
* 5.图片背景:改变 $red $green $blue 三个成员变量的值即可 
**/ 
$an = new Authnum(); 
$an->ext_num_type=''; 
$an->ext_pixel = true; //干扰点 
$an->ext_line = false; //干扰线 
$an->ext_rand_y= true; //Y轴随机 
$an->green = 238; 
$an->create(); 
?>
PHP 相关文章推荐
PHP用户指南-cookies部分
Oct 09 PHP
php 修改zen-cart下单和付款流程以防止漏单
Mar 08 PHP
PHP网站安装程序制作的原理、步骤、注意事项和示例代码
Aug 01 PHP
win7计划任务定时执行PHP脚本设置图解
May 09 PHP
laravel 4安装及入门图文教程
Oct 29 PHP
PHP获取youku视频真实flv文件地址的方法
Dec 23 PHP
php中有关合并某一字段键值相同的数组合并的改进
Mar 10 PHP
PHP实现简单汉字验证码
Jul 28 PHP
thinkPHP简单遍历数组方法分析
May 16 PHP
详解WordPress中添加友情链接的方法
May 21 PHP
php自定义函数实现JS的escape的方法示例
Jul 07 PHP
yii2.0框架场景的简单使用示例
Jan 25 PHP
PHP项目开发中最常用的自定义函数整理
Dec 02 #PHP
PHP自动选择 连接本地还是远程数据库
Dec 02 #PHP
Mysql数据库操作类( 1127版,提供源码下载 )
Dec 02 #PHP
PHP分页函数代码(简单实用型)
Dec 02 #PHP
php图片处理:加水印、缩略图的实现(自定义函数:watermark、thumbnail)
Dec 02 #PHP
php小偷相关截取函数备忘
Nov 28 #PHP
php与paypal整合方法
Nov 28 #PHP
You might like
PHP实现显示照片exif信息的方法
2014/07/11 PHP
php格式化金额函数分享
2015/02/02 PHP
php打造智能化的柱状图程序,用于报表等
2015/06/19 PHP
Yii2.0实现的批量更新及批量插入功能示例
2019/01/29 PHP
Javascript 面向对象 继承
2010/05/13 Javascript
jQueryUI如何自定义组件实现代码
2010/11/14 Javascript
JavaScript动态创建link标签到head里的方法
2014/12/22 Javascript
jQuery中append()方法用法实例
2014/12/25 Javascript
javascript每日必学之条件分支
2016/02/17 Javascript
js剪切板应用clipboardData实例解析
2016/05/29 Javascript
js实现四舍五入完全保留两位小数的方法
2016/08/02 Javascript
BOM系列第三篇之定时器应用(时钟、倒计时、秒表和闹钟)
2016/08/17 Javascript
jQuery插件FusionCharts绘制ScrollColumn2D图效果示例【附demo源码下载】
2017/03/22 jQuery
vue使用Axios做ajax请求详解
2017/06/07 Javascript
webpack-dev-server自动更新页面方法
2018/02/22 Javascript
详解react关于事件绑定this的四种方式
2018/03/09 Javascript
vue组件name的作用小结
2018/05/23 Javascript
vue实现Input输入框模糊查询方法
2021/01/29 Javascript
python zip文件 压缩
2008/12/24 Python
简单上手Python中装饰器的使用
2015/07/12 Python
Python写的一个定时重跑获取数据库数据
2016/12/28 Python
python django事务transaction源码分析详解
2017/03/17 Python
Python中with及contextlib的用法详解
2017/06/08 Python
Django中提供的6种缓存方式详解
2019/08/05 Python
python通过txt文件批量安装依赖包的实现步骤
2019/08/13 Python
Python列表切片常用操作实例解析
2019/12/16 Python
django ListView的使用 ListView中获取url中的参数值方式
2020/03/27 Python
keras读取h5文件load_weights、load代码操作
2020/06/12 Python
python 调用API接口 获取和解析 Json数据
2020/09/28 Python
荷兰本土平价百货:HEMA
2017/10/23 全球购物
Internet体系结构
2014/12/21 面试题
信息技术课后反思
2014/04/27 职场文书
优质护理服务演讲稿
2014/05/07 职场文书
人身损害赔偿协议书
2016/03/22 职场文书
想要创业,那么你做好准备了吗?
2019/07/01 职场文书
python代码实现备忘录案例讲解
2021/07/26 Python