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 相关文章推荐
PHP5.0对象模型探索之抽象方法和抽象类
Sep 05 PHP
PHP 向右侧拉菜单实现代码,测试使用中
Nov 03 PHP
在PHP中使用反射技术的架构插件使用说明
May 18 PHP
hadoop中一些常用的命令介绍
Jun 19 PHP
PHP中创建图像并绘制文字的例子
Nov 19 PHP
PHP中把有符号整型转换为无符号整型方法
May 27 PHP
Docker 如何布置PHP开发环境
Jun 21 PHP
thinkphp项目如何自定义微信分享描述内容
Feb 20 PHP
PHP排序算法之直接插入排序(Straight Insertion Sort)实例分析
Apr 20 PHP
php代码调试利器firephp安装与使用方法分析
Aug 21 PHP
PHP的介绍以及优势详细分析
Sep 05 PHP
laravel使用redis队列实例讲解
Mar 23 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
S900/ ETON E1-XM 收音机
2021/03/02 无线电
php中关于codeigniter的xmlrpc的类在进行数据交换时的类型问题
2011/07/03 PHP
PHP判断上传文件类型的解决办法
2015/10/20 PHP
Nigma vs Alliance BO5 第一场2.14
2021/03/10 DOTA
iframe里使用JavaScript控制主页转向的方法
2015/04/03 Javascript
javascript自定义右键弹出菜单实现方法
2015/05/25 Javascript
JavaScript中全选、全不选、反选、无刷新删除、批量删除、即点即改入库(在yii框架中操作)的代码分享
2016/11/01 Javascript
微信小程序 省市区选择器实例详解(附源码下载)
2017/01/05 Javascript
angular ng-click防止重复提交实例
2017/06/16 Javascript
vue.js默认路由不加载linkActiveClass问题的解决方法
2017/12/11 Javascript
使用 vue.js 构建大型单页应用
2018/02/10 Javascript
vue 指定组件缓存实例详解
2018/04/01 Javascript
vue踩坑记录之数组定义和赋值问题
2019/03/20 Javascript
vue 检测用户上传图片宽高的方法
2020/02/06 Javascript
快速了解Vue父子组件传值以及父调子方法、子调父方法
2020/07/15 Javascript
一行JavaScript代码如何实现瀑布流布局
2020/12/11 Javascript
python算法学习之基数排序实例
2013/12/18 Python
Linux下使用python调用top命令获得CPU利用率
2015/03/10 Python
Python random模块用法解析及简单示例
2017/12/18 Python
PyQt5每天必学之切换按钮
2020/08/20 Python
Python subprocess模块常见用法分析
2018/06/12 Python
树莓派使用python-librtmp实现rtmp推流h264的方法
2019/07/22 Python
python写一个随机点名软件的实例
2019/11/28 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
2020/01/16 Python
Python编程快速上手——Excel表格创建乘法表案例分析
2020/02/28 Python
通过Python pyecharts输出保存图片代码实例
2020/11/25 Python
pytorch 中forward 的用法与解释说明
2021/02/26 Python
惠普墨西哥官方商店:HP墨西哥
2016/12/01 全球购物
英国婴儿及儿童产品商店:TigerParrot
2019/03/04 全球购物
华为消费者德国官方网站:HUAWEI德国
2020/11/03 全球购物
一些Unix笔试题和面试题
2012/09/25 面试题
求职简历的自我评价
2014/01/31 职场文书
给面试官的感谢信
2014/02/01 职场文书
2014年导购员工作总结
2014/11/18 职场文书
2015年政教主任工作总结
2015/07/23 职场文书
OpenCV-Python实现轮廓的特征值
2021/06/09 Python