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远程连接MYSQL数据库非常慢的解决方法
Jul 05 PHP
php 使用post,get的一种简洁方式
Apr 25 PHP
php实现单链表的实例代码
Mar 22 PHP
PHP中array_slice函数用法实例详解
Nov 25 PHP
php计算两个整数的最大公约数常用算法小结
Mar 05 PHP
PHP实现获取某个月份周次信息的方法
Aug 11 PHP
php ucwords() 函数将字符串中每个单词的首字符转换为大写(实现代码)
May 12 PHP
Yii2创建多界面主题(Theme)的方法
Oct 08 PHP
详解php 使用Callable Closure强制指定回调类型
Oct 26 PHP
laravel中的一些简单实用功能
Nov 03 PHP
JS操作XML中DTD介绍及使用方法分析
Jul 04 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
基于文本的访客签到簿
2006/10/09 PHP
珊瑚虫IP库浅析
2007/02/15 PHP
PHP session常见问题集锦及解决办法总结
2007/03/18 PHP
Laravel 读取 config 下的数据方法
2019/10/13 PHP
js textarea自动增高并隐藏滚动条
2009/12/16 Javascript
jQuery对下拉框,单选框,多选框的操作
2014/02/21 Javascript
jQuery+ajax实现鼠标单击修改内容的思路
2014/06/29 Javascript
基于javascript实现listbox左右移动
2016/01/29 Javascript
如何在AngularJs中调用第三方插件库
2017/05/21 Javascript
基于jQuery实现定位导航位置效果
2017/11/15 jQuery
bmob js-sdk 在vue中的使用教程
2018/01/21 Javascript
Vue实现日历小插件
2019/06/26 Javascript
three.js欧拉角和四元数的使用方法
2020/07/26 Javascript
复制粘贴功能的Python程序
2008/04/04 Python
简单介绍Python中的RSS处理
2015/04/13 Python
Bottle框架中的装饰器类和描述符应用详解
2017/10/28 Python
详解Python里使用正则表达式的ASCII模式
2017/11/02 Python
python微信公众号之关注公众号自动回复
2018/10/25 Python
python数据处理之如何选取csv文件中某几行的数据
2019/09/02 Python
Python正则表达式学习小例子
2020/03/03 Python
解决pycharm编辑区显示yaml文件层级结构遇中文乱码问题
2020/04/27 Python
解决pytorch 的state_dict()拷贝问题
2021/03/03 Python
美国购买当代和现代家具网站:MODTEMPO
2018/07/20 全球购物
在加拿大在线租赁和购买电子游戏:Game Access
2019/09/02 全球购物
Quiksilver美国官网:始于1969年的优质冲浪服和滑雪板外套
2020/04/20 全球购物
自荐信不宜过于夸大
2013/11/06 职场文书
社区工作感言
2014/02/21 职场文书
二手房买卖协议书
2014/04/10 职场文书
2014年银行客户经理工作总结
2014/11/12 职场文书
文明单位申报材料
2014/12/23 职场文书
财务经理岗位职责范本
2015/04/08 职场文书
少年的你:世界上没有如果,要在第一次就勇敢的反抗
2019/11/20 职场文书
Vue+Element UI实现概要小弹窗的全过程
2021/05/30 Vue.js
「月刊Comic Alive」2022年5月号封面公开
2022/03/21 日漫
《宝可梦》动画制作25周年到来 官方发布特别纪念视频
2022/04/01 日漫
Java Spring Lifecycle的使用
2022/05/06 Java/Android