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下利用shell后台运行PHP脚本,并获取该脚本的Process ID的代码
Sep 19 PHP
Trying to clone an uncloneable object of class Imagic的解决方法
Jan 11 PHP
php 模拟POST提交的2种方法详解
Jun 17 PHP
php function用法如何递归及return和echo区别
Mar 07 PHP
php使用PDO操作MySQL数据库实例
Dec 30 PHP
php从完整文件路径中分离文件目录和文件名的方法
Mar 13 PHP
php+jQuery+Ajax实现点赞效果的方法(附源码下载)
Jul 21 PHP
Yii2实现跨mysql数据库关联查询排序功能代码
Feb 10 PHP
php查找字符串中第一个非0的位置截取
Feb 27 PHP
wordpress网站转移到本地运行测试的方法
Mar 15 PHP
PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法
Nov 10 PHP
Yii 使用intervention/image拓展实现图像处理功能
Jun 22 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和ACCESS写聊天室(五)
2006/10/09 PHP
深入了解PHP类Class的概念
2012/06/14 PHP
linux下编译安装memcached服务
2014/08/03 PHP
javascript replace方法与正则表达式
2008/02/19 Javascript
ext监听事件方法[初级篇]
2008/04/27 Javascript
javascript preload&amp;lazy load
2010/05/13 Javascript
jQuery弹出(alert)select选择的值
2013/04/21 Javascript
js循环改变div颜色具体方法
2013/06/25 Javascript
JavaScript回调(callback)函数概念自我理解及示例
2013/07/04 Javascript
Extjs实现进度条的两种便捷方式
2013/09/26 Javascript
js脚本实现数据去重
2014/11/27 Javascript
nodejs导出excel的方法
2015/06/30 NodeJs
解析预加载显示图片艺术
2016/12/05 Javascript
自带气泡提示的vue校验插件(vue-verify-pop)
2017/04/07 Javascript
手把手搭建安装基于windows的Vue.js运行环境
2017/06/12 Javascript
基于vue.js无缝滚动效果
2018/01/25 Javascript
angular2实现统一的http请求头方法
2018/08/13 Javascript
Node登录权限验证token验证实现的方法示例
2020/05/25 Javascript
Vue中父子组件的值传递与方法传递
2020/09/28 Javascript
[01:05:00]2018国际邀请赛 表演赛 Pain vs OpenAI
2018/08/24 DOTA
Python设计模式之命令模式简单示例
2018/01/10 Python
python paramiko远程服务器终端操作过程解析
2019/12/14 Python
django有外键关系的两张表如何相互查找
2020/02/10 Python
在Python中通过threshold创建mask方式
2020/02/19 Python
django表单中的按钮获取数据的实例分析
2020/07/31 Python
皇家阿尔伯特瓷器美国官网:Royal Albert美国
2020/02/16 全球购物
上班迟到检讨书
2014/01/10 职场文书
骨干教师培训制度
2014/01/13 职场文书
2014新年寄语
2014/01/20 职场文书
玲玲的画教学反思
2014/02/04 职场文书
小学毕业感言300字
2014/02/19 职场文书
保护水资源的标语
2014/06/17 职场文书
2014年少先队工作总结
2014/12/03 职场文书
承诺保证书格式
2015/02/28 职场文书
用人单位的规章制度,怎样制定才是有效的?
2019/07/09 职场文书
Python中rapidjson参数校验实现
2021/07/25 Python