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 相关文章推荐
1.PHP简介
Oct 09 PHP
PHP4与PHP5的时间格式问题
Feb 17 PHP
snoopy 强大的PHP采集类使用实例代码
Dec 09 PHP
PHP静态调用非静态方法的应用分析
May 02 PHP
PHP定时更新程序设计思路分享
Jun 10 PHP
php使用unset()删除数组中某个单元(键)的方法
Feb 17 PHP
Yii视图CGridView实现操作按钮定义地址示例
Jul 14 PHP
PHP对象、模式与实践之高级特性分析
Dec 08 PHP
Symfony查询方法实例小结
Jun 28 PHP
php检查函数必传参数是否存在的实例详解
Aug 28 PHP
浅谈Laravel核心解读之Console内核
Dec 02 PHP
YII2.0框架行为(Behavior)深入详解
Jul 26 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
THINKPHP内容分页代码分享
2015/01/14 PHP
利用PHP获取访客IP、地区位置、浏览器及来源页面等信息
2017/06/27 PHP
php mysql PDO 查询操作的实例详解
2017/09/23 PHP
php+js实现裁剪任意形状图片
2018/10/31 PHP
HTML-CSS群中单选引发的“事件”
2007/03/05 Javascript
可以显示单图片,多图片ajax请求的ThickBox3.1类下载
2007/12/23 Javascript
让JavaScript 轻松支持函数重载 (Part 1 - 设计)
2009/08/04 Javascript
jQuery的三种$()
2009/12/30 Javascript
JavaScript初学者应注意的七个细节小结
2012/01/30 Javascript
解决Extjs4中form表单提交后无法进入success函数问题
2013/11/26 Javascript
js判断上传文件类型判断FileUpload文件类型代码
2014/05/20 Javascript
鼠标悬浮显示二级菜单效果的jquery实现
2014/10/29 Javascript
jQuery插件datalist实现很好看的input下拉列表
2015/07/14 Javascript
javascript中substring()、substr()、slice()的区别
2015/08/30 Javascript
BootStrap 动态添加验证项和取消验证项的实现方法
2016/09/28 Javascript
vue.js移动端app实战1:初始配置详解
2017/07/24 Javascript
Vue源码解析之Template转化为AST的实现方法
2018/12/14 Javascript
vue实现百度下拉列表交互操作示例
2019/03/12 Javascript
原生JS实现微信通讯录
2020/06/18 Javascript
js实现查询商品案例
2020/07/22 Javascript
VUE项目axios请求头更改Content-Type操作
2020/07/24 Javascript
原生js实现购物车功能
2020/09/23 Javascript
解决vue-cli输入命令vue ui没效果的问题
2020/11/17 Javascript
JS实现简易日历效果
2021/01/25 Javascript
[38:21]2014 DOTA2国际邀请赛中国区预选赛5.21 TongFu VS LGD-CDEC
2014/05/22 DOTA
Python 判断 有向图 是否有环的实例讲解
2018/02/01 Python
对pandas中apply函数的用法详解
2018/04/10 Python
transform python环境快速配置方法
2018/09/27 Python
基于python3抓取pinpoint应用信息入库
2020/01/08 Python
tensorflow 实现自定义梯度反向传播代码
2020/02/10 Python
英国领先的在线高尔夫商店:Scottsdale Golf
2019/08/26 全球购物
用C#语言写出与SQLSERVER访问时的具体过程
2013/04/16 面试题
化工机械应届生求职信
2013/11/04 职场文书
药剂专业自荐信范文
2014/04/16 职场文书
行政求职信
2014/07/04 职场文书
幼儿园开学报名通知
2015/07/16 职场文书