PHP验证码类代码( 最新修改,完全定制化! )

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();
?>

时间: 2024-08-29 07:18:01

PHP验证码类代码( 最新修改,完全定制化! )的相关文章

PHP验证码类代码( 最新修改,完全定制化! )_php技巧

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 $

一个PHP验证码类代码分享(已封装成类)_php技巧

复制代码 代码如下: <?php session_start(); Header("Content-type: image/gif"); class SecurityCode { private $codes = ''; function __construct() { $code = '0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z'; $codeArray = explode('-

PHP 动态随机生成验证码类代码_php技巧

下面是效果图,这个效果图是没有开启干扰码的效果图 下面是类代码 复制代码 代码如下: <?php /************************************************ //FILE:ImageCode //DONE:生成动态验证码类 //DATE"2010-3-31 //Author:www.5dkx.com 5D开心博客 *********************************************************************

php生成验证码类代码

class securecode {     private static $instance=null;     private $code = '';     private $fontfile;     private $validate;     private $image;     private $specialadd = 'special string for securecode';     private $codeexpire=86400;     private $cod

实用PHP验证码类代码(1/2)

<?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; pu

安防定制化服务的机遇与挑战

随着安防产品应用范围越来越广,应用环境多样,用户对生产企业的产品也提出新要求和挑战.客户希望得到更为便捷智能的产品及全面的配套服务,因此定制化也是一个行业发展成熟的必然趋势.从单纯卖产品到产品功能定制再到产品+整体解决方案,甚至到产品选型等,是对客户服务品质的提升,也是对企业综合应变能力的挑战. 定制化更多地会体现在行业市场,尤其是安防或视频业务与其自身行业业务紧密结合的市场,而这一市场正是典型的解决方案市场.所以,从产品和解决方案两者来看,产品更偏向于标准化,解决方案更侧重于定制化.从企业经营

java生成图片验证码实例代码_java

关于java图片验证码的文章最近更新了不少,帮助大家掌握java验证码的生成技术,下文为大家分享了java生成图片验证码最简单的方法,供大家参考. 现在各行业在定制系统时都会考虑到机器注册,现在最有效的方式就是输入验证.现在的验证方式有很多种: 一.问题验证,其实也是图片验证,在图片上生成问题,然后输入框输入答案. 二.图片验证,输入图片上展示的文字信息. 三.短信验证,比较繁杂,用户也不怎么喜欢. 四.还有就是百度最新的验证方式.图片上生成文字,然后出现一个文字点击框,选择你在验证图片上看到的

封装的一个asp.net验证码类

第一个反映是验证码的这些属性是否可以设置(也就是说是封装成一个类,然后以暴露公有属性和方法的方式来使用的,还是在直接在一般处理程序中实现了验证码的生成到输出),结果比较可惜,是后者...... 里面的算法感觉挺不错,至少对于我这种对算法几乎是不懂的人来说是这样,于是想到去封装一下她然后无耻地纳入自己的类库中去^^ old原文件如下:点击下载 首先分析一下这份代码中的不足(自己觉得的): 1.面向过程式的编程,如果要修改验证码的属性,例如修改字体的大小.背景颜色等细节的东西,需要去一般处理程序中找

ASP.NET MVC+EF框架+EasyUI实现权限管理系列(11)-验证码实现和底层修改

原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(11)-验证码实现和底层修改 ASP.NET MVC+EF框架+EasyUI实现权限管系列  (开篇)   (1):框架搭建    (2):数据库访问层的设计Demo    (3):面向接口编程   (4 ):业务逻辑层的封装   (5):前台Jquery easyUI实现    (6):EF上下文实例管理    (7):DBSession的封装   (8):DBSession线程内唯一    (9):TT摸版的学习   (1