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 相关文章推荐
zend framework多模块多布局配置
Feb 26 PHP
深入php 正则表达式的学习探讨
Jun 06 PHP
php验证session无效的解决方法
Nov 04 PHP
Linux系统递归生成目录中文件的md5的方法
Jun 29 PHP
利用PHP将部分内容用星号替换
Apr 21 PHP
Yii框架结合sphinx,Ajax实现搜索分页功能示例
Oct 18 PHP
PHP中串行化用法示例
Nov 16 PHP
PHP基于自定义类随机生成姓名的方法示例
Aug 05 PHP
PHP实现求解最长公共子串问题的方法
Nov 17 PHP
thinkPHP框架实现多表查询的方法
Jun 14 PHP
在Laravel中使用GuzzleHttp调用第三方服务的API接口代码
Oct 15 PHP
php优化查询foreach代码实例讲解
Mar 24 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
MySql 按时间段查询数据方法(实例说明)
2008/11/02 PHP
使用PHP会话(Session)实现用户登陆功能
2013/06/29 PHP
WordPres对前端页面调试时的两个PHP函数使用小技巧
2015/12/22 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
2018/02/11 PHP
详解php命令注入攻击
2019/04/06 PHP
jquery网页元素拖拽插件效果及实现
2013/08/05 Javascript
JS取文本框中最小值的简单实例
2013/11/29 Javascript
javascript window.open打开新窗口后无法再次打开该窗口问题的解决方法
2014/04/12 Javascript
在JS中解析HTML字符串示例代码
2014/04/16 Javascript
jQuery实现图片预加载效果
2015/11/27 Javascript
JS未跨域操作iframe里的DOM
2016/06/01 Javascript
浅谈键盘上回车按钮的js触发事件
2017/02/13 Javascript
Input文本框随着输入内容多少自动延伸的实现
2017/02/15 Javascript
通过 JS 判断页面是否有滚动条的实现方法
2018/04/05 Javascript
Vue cli构建及项目打包以及出现的问题解决
2018/08/27 Javascript
原生js实现移动端Touch轮播图的方法步骤
2019/01/03 Javascript
Vue-router 报错NavigationDuplicated的解决方法
2020/03/31 Javascript
[02:55]DOTA2英雄基础教程 发条技师
2013/12/04 DOTA
跟老齐学Python之从格式化表达式到方法
2014/09/28 Python
python实现给数组按片赋值的方法
2015/07/28 Python
Python面向对象之类和对象属性的增删改查操作示例
2018/12/14 Python
Python列表对象实现原理详解
2019/07/01 Python
浅谈Python中threading join和setDaemon用法及区别说明
2020/05/02 Python
HTML5轻松实现全屏视频背景的示例
2018/04/23 HTML / CSS
Superdry极度干燥美国官网:英国制造的服装品牌
2018/11/13 全球购物
阿巴庭院:Abba Patio
2019/06/18 全球购物
英国网上自行车商店:Tredz Bikes
2019/10/29 全球购物
北京麒麟网信息技术有限公司网络游戏测试面试题
2013/09/28 面试题
2014年公司庆元旦活动方案
2014/03/05 职场文书
卖房授权委托书样本
2014/10/05 职场文书
群众路线自我剖析及整改措施
2014/11/04 职场文书
2014年销售工作总结范文
2014/12/01 职场文书
公司出纳岗位职责
2015/03/31 职场文书
Mysql 如何批量插入数据
2021/04/06 MySQL
Python快速优雅的批量修改Word文档样式
2021/05/20 Python
浅析JavaScript中的变量提升
2022/06/01 Javascript