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 相关文章推荐
一个简单的php实现的MySQL数据浏览器
Mar 11 PHP
谨慎使用PHP的引用原因分析
Sep 06 PHP
让CodeIgniter的ellipsize()支持中文截断的方法
Jun 12 PHP
destoon实现商铺管理主页设置增加新菜单的方法
Jun 26 PHP
PHP对文件夹递归执行chmod命令的方法
Jun 19 PHP
php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
Jul 06 PHP
PHP Web木马扫描器代码分享
Sep 06 PHP
Yii2搭建后台并实现rbac权限控制完整实例教程
Apr 28 PHP
php下载文件,添加响应头的简单实例
Sep 22 PHP
PHP数据的提交与过滤基本操作实例详解
Nov 11 PHP
php生成二维码不保存服务器还有下载功能的实现代码
Aug 09 PHP
Laravel 自动生成验证的实例讲解:login / logout
Oct 14 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下过滤HTML代码的函数
2007/12/10 PHP
PHP设计模式之调解者模式的深入解析
2013/06/13 PHP
php读取文件内容的几种方法详解
2013/06/26 PHP
PHP采用自定义函数实现遍历目录下所有文件的方法
2014/08/19 PHP
PHP编程实现的TCP服务端和客户端功能示例
2018/04/13 PHP
Linux下安装Memcached服务器和客户端与PHP使用示例
2019/04/15 PHP
javascript 函数速查表
2010/02/07 Javascript
JS延迟加载(setTimeout) JS最后加载
2010/07/15 Javascript
jQuery点击自身以外地方关闭弹出层的简单实例
2013/12/24 Javascript
jQuery如何取id有.的值一般的方法是取不到的
2014/04/18 Javascript
使用原生js封装webapp滑动效果(惯性滑动、滑动回弹)
2014/05/06 Javascript
js字符串完全替换函数分享
2014/12/03 Javascript
jQuery中 delegate使用的问题
2015/07/03 Javascript
教你JS中的运算符乘方、开方及变量格式转换
2016/08/09 Javascript
详细解读Jquery各Ajax函数($.get(),$.post(),$.ajax(),$.getJSON())
2016/08/15 Javascript
Angularjs使用ng-repeat中$even和$odd属性的注意事项
2016/12/31 Javascript
jQuery选择器之属性过滤选择器详解
2017/09/28 jQuery
vue使用vuex实现首页导航切换不同路由的方法
2019/05/08 Javascript
react 组件传值的三种方法
2019/06/03 Javascript
使用konva和vue-konva库实现拖拽滑块验证功能
2020/04/27 Javascript
python实现哈希表
2014/02/07 Python
浅谈Python中的zip()与*zip()函数详解
2018/02/24 Python
Python 词典(Dict) 加载与保存示例
2019/12/06 Python
python为QT程序添加图标的方法详解
2020/03/09 Python
如何基于windows实现python定时爬虫
2020/05/01 Python
Keras中的两种模型:Sequential和Model用法
2020/06/27 Python
Python如何实现远程方法调用
2020/08/07 Python
html5开发三八女王节表白神器
2018/03/07 HTML / CSS
AmazeUI的下载配置与Helloworld的实现
2020/08/19 HTML / CSS
JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
2013/07/02 面试题
浪漫婚礼主题活动策划方案
2014/09/15 职场文书
模范班主任事迹材料
2014/12/17 职场文书
党风廉政建设调研报告
2015/01/01 职场文书
2015年秋季灭鼠工作总结
2015/07/27 职场文书
高中体育课教学反思
2016/02/16 职场文书
MySQL索引失效场景及解决方案
2022/07/23 MySQL