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 相关文章推荐
BBS(php &amp; mysql)完整版(三)
Oct 09 PHP
PHP4 与 MySQL 交互使用
Oct 09 PHP
非常好用的两个PHP函数 serialize()和unserialize()
Feb 04 PHP
php&amp;mysql 日期操作小记
Feb 27 PHP
PHP入门之常量简介和系统常量
May 12 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
Jun 25 PHP
php找出指定范围内回文数且平方根也是回文数的方法
Mar 23 PHP
PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
Sep 22 PHP
PHP获取访问页面HTTP状态码的实现代码
Nov 03 PHP
PHP序列化的四种实现方法与横向对比
Nov 29 PHP
在Ubuntu 18.04上安装PHP 7.3 7.2和7.0的方法
Apr 09 PHP
PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
Dec 20 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
在smarty模板中使用PHP函数的方法
2011/04/23 PHP
PHP函数http_build_query使用详解
2014/08/20 PHP
php获取发送给用户的header信息的方法
2015/03/16 PHP
PHP实现登录验证码校验功能
2018/05/17 PHP
jQuery 方法大全方便学习参考
2010/02/25 Javascript
学习面向对象之面向对象的术语
2010/11/30 Javascript
javascript 基础篇4 window对象,DOM
2012/03/14 Javascript
jquery无法为动态生成的元素添加点击事件的解决方法(推荐)
2016/12/26 Javascript
js+css3实现旋转效果
2017/01/20 Javascript
canvas绘制一个常用的emoji表情
2017/03/30 Javascript
vue webpack实用技巧总结
2018/04/24 Javascript
ES6 Object属性新的写法实例小结
2019/06/25 Javascript
Jquery滑动门/tab切换实现方法完整示例
2020/06/05 jQuery
vue 内联样式style中的background用法说明
2020/08/05 Javascript
[01:23]2014DOTA2国际邀请赛 球迷无处不在Ti现场世界杯受关注
2014/07/10 DOTA
[01:13:08]2018DOTA2亚洲邀请赛4.6 淘汰赛 mineski vs LGD 第二场
2018/04/10 DOTA
理解Python中的With语句
2016/03/18 Python
pandas.cut具体使用总结
2019/06/24 Python
Python标准库json模块和pickle模块使用详解
2020/03/10 Python
python ETL工具 pyetl
2020/06/07 Python
详解pyinstaller生成exe的闪退问题解决方案
2020/06/19 Python
Django自带用户认证系统使用方法解析
2020/11/12 Python
使用Pytorch搭建模型的步骤
2020/11/16 Python
基于python的opencv图像处理实现对斑马线的检测示例
2020/11/29 Python
实习生个人找工作的自我评价
2013/10/30 职场文书
经销商订货会主持词
2014/03/27 职场文书
《白鹅》教学反思
2014/04/13 职场文书
2014五一国际劳动节活动总结范文
2014/04/14 职场文书
工作推荐信范文
2014/05/10 职场文书
竞赛口号大全
2014/06/16 职场文书
建筑安全生产目标责任书
2014/07/23 职场文书
第二批党的群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
党员民主评议个人总结
2014/10/20 职场文书
兵马俑导游词
2015/02/02 职场文书
2015年爱牙日活动总结
2015/02/05 职场文书
2016年秋季开学典礼新闻稿
2015/11/25 职场文书