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:风雨欲来 路在何方?
Oct 09 PHP
一个查看session内容的函数
Oct 09 PHP
ajax缓存问题解决途径
Dec 06 PHP
PHP判断图片格式的七种方法小结
Jun 03 PHP
探讨file_get_contents与curl效率及稳定性的分析
Jun 06 PHP
PHP关于IE下的iframe跨域导致session丢失问题解决方法
Oct 10 PHP
md5 16位二进制与32位字符串相互转换示例
Dec 30 PHP
一个经典实用的PHP图像处理类分享
Nov 18 PHP
浅谈php7的重大新特性
Oct 23 PHP
Symfony2 session用法实例分析
Feb 04 PHP
php 输出json及显示json中的中文汉字详解及实例
Nov 09 PHP
php接口技术实例详解
Dec 07 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实现小型站点广告管理
2006/10/09 PHP
php快速url重写 更新版[需php 5.30以上]
2010/04/20 PHP
php中static静态变量的使用方法详解
2010/06/04 PHP
PHP sprintf() 函数的应用(定义和用法)
2012/06/29 PHP
深入PHP运行环境配置的详解
2013/06/04 PHP
PHP jQuery+Ajax结合写批量删除功能
2017/05/19 PHP
扩展Jquery插件处理mouseover时内部有子元素时发生样式闪烁
2011/12/08 Javascript
node.js中使用q.js实现api的promise化
2014/09/17 Javascript
javascript学习笔记(六)数据类型和JSON格式
2014/10/08 Javascript
jquery实现清新实用的网页菜单效果
2015/08/28 Javascript
JavaScript多图片上传案例
2015/09/28 Javascript
实时监控input框,实现输入框与下拉框联动的实例
2018/01/23 Javascript
JavaScript+Canvas实现彩色图片转换成黑白图片的方法分析
2018/07/31 Javascript
vue如何进行动画的封装
2018/09/26 Javascript
koa大型web项目中使用路由装饰器的方法示例
2019/04/02 Javascript
微信小程序从注册账号到上架(图文详解)
2019/07/17 Javascript
微信小程序使用echarts获取数据并生成折线图
2019/10/16 Javascript
原生JavaScript实现滑动拖动验证的示例代码
2019/12/06 Javascript
利用 JavaScript 实现并发控制的示例代码
2020/12/31 Javascript
Python解析网页源代码中的115网盘链接实例
2014/09/30 Python
KMP算法精解及其Python版的代码示例
2016/06/01 Python
python去除字符串中的换行符
2017/10/11 Python
分享Pycharm中一些不为人知的技巧
2018/04/03 Python
python如何统计代码运行的时长
2019/07/24 Python
python 判断三个数字中的最大值实例代码
2019/07/24 Python
Levi’s美国官网:美国著名的牛仔裤品牌
2016/08/19 全球购物
定制iPhone和Macbook保护壳:Slick Case
2018/11/21 全球购物
Hoover胡佛官网:美国吸尘器和洗地机品牌
2019/01/09 全球购物
时尚圣经:The Fashion Bible
2019/03/03 全球购物
历史系自荐信范文
2013/12/24 职场文书
财务专业大学生职业生涯规划范文
2013/12/30 职场文书
秋季运动会通讯稿
2014/01/24 职场文书
副护士长竞聘演讲稿
2014/04/30 职场文书
关于梦想的演讲稿
2014/05/05 职场文书
基于Python的EasyGUI学习实践
2021/05/07 Python
输入框跟随文字内容适配宽实现示例
2022/08/14 Javascript