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 相关文章推荐
网站当前的在线人数
Oct 09 PHP
php截取后台登陆密码的代码
May 05 PHP
PHP设计模式之责任链模式的深入解析
Jun 13 PHP
PHP用strstr()函数阻止垃圾评论(通过判断a标记)
Sep 28 PHP
PHP列出MySQL中所有数据库的方法
Mar 12 PHP
检测codeigniter脚本消耗内存情况的方法
Mar 21 PHP
Yii2 rbac权限控制之菜单menu实例教程
Apr 28 PHP
PHP简单实现文本计数器的方法
Apr 28 PHP
PHP的AES加密算法完整实例
Jul 20 PHP
php正则提取html图片(img)src地址与任意属性的方法
Feb 08 PHP
PHP随机数函数rand()与mt_rand()的讲解
Mar 25 PHP
Yii2框架中一些折磨人的坑
Dec 15 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 命令行参数详解及应用
2011/05/18 PHP
php中多维数组按指定value排序的实现代码
2014/08/19 PHP
php简单实现多字节字符串翻转的方法
2015/03/31 PHP
Laravel执行migrate命令提示:No such file or directory的解决方法
2016/03/16 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
各种效果的jquery ui(接口)介绍
2008/09/17 Javascript
js实现浏览器的各种菜单命令比如打印、查看源文件等等
2013/10/24 Javascript
JS中的数组的sort方法使用示例
2014/01/22 Javascript
动态加载JavaScript文件的两种方法
2016/04/22 Javascript
深入剖析JavaScript面向对象编程
2016/07/12 Javascript
jQuery实现的简单动态添加、删除表格功能示例
2017/09/21 jQuery
快速搭建vue2.0+boostrap项目的方法
2018/04/09 Javascript
bootstrap实现点击删除按钮弹出确认框的实例代码
2018/08/16 Javascript
用Object.prototype.toString.call(obj)检测对象类型原因分析
2018/10/11 Javascript
JS字典Dictionary类定义与用法示例
2019/02/01 Javascript
JavaScript面试技巧之数组的一些不low操作
2019/03/22 Javascript
基于python的汉字转GBK码实现代码
2012/02/19 Python
基于numpy.random.randn()与rand()的区别详解
2018/04/17 Python
python3 遍历删除特定后缀名文件的方法
2018/04/23 Python
Pycharm以root权限运行脚本的方法
2019/01/19 Python
python实现连连看辅助之图像识别延伸
2019/07/17 Python
运用PyTorch动手搭建一个共享单车预测器
2019/08/06 Python
PyChon中关于Jekins的详细安装(推荐)
2020/12/28 Python
css3实现一款模仿iphone样式的注册表单
2013/03/20 HTML / CSS
HTML5实现QQ聊天气泡效果
2017/06/26 HTML / CSS
Html5元素及基本语法详解
2016/08/02 HTML / CSS
希尔顿酒店中国网站:Hilton中国
2017/03/11 全球购物
WWE美国职业摔角官方商店:WWE Shop
2018/11/15 全球购物
What's the difference between an interface and abstract class? (接口与抽象类有什么区别)
2012/10/29 面试题
新年寄语大全
2014/04/12 职场文书
2014县政府领导班子对照检查材料思想汇报
2014/09/25 职场文书
寒暑假实习证明书模板
2014/11/29 职场文书
2014年煤矿安全工作总结
2014/12/04 职场文书
创业计划书之少年玩具店
2019/09/05 职场文书
教你怎么用Python操作MySql数据库
2021/05/31 Python
Python基础之变量的相关知识总结
2021/06/23 Python