PHP驗(yàn)證碼類代碼( 最新修改,完全定制化! )
更新時(shí)間:2010年12月02日 22:11:20 作者:
PHP驗(yàn)證碼類代碼,需要的朋友可以參考下。
Authnum.class.php 下載
<?php
session_start();
class Authnum {
//圖片對象、寬度、高度、驗(yàn)證碼長度
private $im;
private $im_width;
private $im_height;
private $len;
//隨機(jī)字符串、y軸坐標(biāo)值、隨機(jī)顏色
private $randnum;
private $y;
private $randcolor;
//背景色的紅綠藍(lán),默認(rèn)是淺灰色
public $red=238;
public $green=238;
public $blue=238;
/**
* 可選設(shè)置:驗(yàn)證碼類型、干擾點(diǎn)、干擾線、Y軸隨機(jī)
* 設(shè)為 false 表示不啟用
**/
//默認(rèn)是大小寫數(shù)字混合型,1 2 3 分別表示 小寫、大寫、數(shù)字型
public $ext_num_type='';
public $ext_pixel = false; //干擾點(diǎn)
public $ext_line = false; //干擾線
public $ext_rand_y= true; //Y軸隨機(jī)
function __construct ($len=4,$im_width='',$im_height=25) {
// 驗(yàn)證碼長度、圖片寬度、高度是實(shí)例化類時(shí)必需的數(shù)據(jù)
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// 設(shè)置圖片背景顏色,默認(rèn)是淺灰色背景
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// 獲得任意位數(shù)的隨機(jī)碼
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àn)證碼圖片Y軸
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
else $this->y = $this->im_height / 4 ;
}
// 獲得隨機(jī)色
function get_randcolor () {
$this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
}
// 添加干擾點(diǎn)
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);
}
}
}
/**創(chuàng)建驗(yàn)證碼圖像:
* 建立畫布(__construct函數(shù))
* 設(shè)置畫布背景($this->set_bgcolor();)
* 獲取隨機(jī)字符串($this->get_randnum ();)
* 文字寫到圖片上(imagestring函數(shù))
* 添加干擾點(diǎn)/線($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
/**使用驗(yàn)證碼類的方法:
* $an = new Authnum(驗(yàn)證碼長度,圖片寬度,圖片高度);
* 實(shí)例化時(shí)不帶參數(shù)則默認(rèn)是四位的60*25尺寸的常規(guī)驗(yàn)證碼圖片
* 表單頁面檢測驗(yàn)證碼的方法,對比 $_SESSION[an] 是否等于 $_POST[驗(yàn)證碼文本框ID]
* 可選配置:
* 1.驗(yàn)證碼類型:$an->ext_num_type=1; 值為1是小寫類型,2是大寫類型,3是數(shù)字類型
* 2.干擾點(diǎn):$an->ext_pixel = false; 值為false表示不添加干擾點(diǎn)
* 3.干擾線:$an->ext_line = false; 值為false表示不添加干擾線
* 4.Y軸隨機(jī):$an->ext_rand_y = false; 值為false表示不支持圖片Y軸隨機(jī)
* 5.圖片背景:改變 $red $green $blue 三個(gè)成員變量的值即可
**/
$an = new Authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //干擾點(diǎn)
$an->ext_line = false; //干擾線
$an->ext_rand_y= true; //Y軸隨機(jī)
$an->green = 238;
$an->create();
?>
復(fù)制代碼 代碼如下:
<?php
session_start();
class Authnum {
//圖片對象、寬度、高度、驗(yàn)證碼長度
private $im;
private $im_width;
private $im_height;
private $len;
//隨機(jī)字符串、y軸坐標(biāo)值、隨機(jī)顏色
private $randnum;
private $y;
private $randcolor;
//背景色的紅綠藍(lán),默認(rèn)是淺灰色
public $red=238;
public $green=238;
public $blue=238;
/**
* 可選設(shè)置:驗(yàn)證碼類型、干擾點(diǎn)、干擾線、Y軸隨機(jī)
* 設(shè)為 false 表示不啟用
**/
//默認(rèn)是大小寫數(shù)字混合型,1 2 3 分別表示 小寫、大寫、數(shù)字型
public $ext_num_type='';
public $ext_pixel = false; //干擾點(diǎn)
public $ext_line = false; //干擾線
public $ext_rand_y= true; //Y軸隨機(jī)
function __construct ($len=4,$im_width='',$im_height=25) {
// 驗(yàn)證碼長度、圖片寬度、高度是實(shí)例化類時(shí)必需的數(shù)據(jù)
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// 設(shè)置圖片背景顏色,默認(rèn)是淺灰色背景
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// 獲得任意位數(shù)的隨機(jī)碼
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àn)證碼圖片Y軸
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
else $this->y = $this->im_height / 4 ;
}
// 獲得隨機(jī)色
function get_randcolor () {
$this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
}
// 添加干擾點(diǎn)
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);
}
}
}
/**創(chuàng)建驗(yàn)證碼圖像:
* 建立畫布(__construct函數(shù))
* 設(shè)置畫布背景($this->set_bgcolor();)
* 獲取隨機(jī)字符串($this->get_randnum ();)
* 文字寫到圖片上(imagestring函數(shù))
* 添加干擾點(diǎn)/線($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
/**使用驗(yàn)證碼類的方法:
* $an = new Authnum(驗(yàn)證碼長度,圖片寬度,圖片高度);
* 實(shí)例化時(shí)不帶參數(shù)則默認(rèn)是四位的60*25尺寸的常規(guī)驗(yàn)證碼圖片
* 表單頁面檢測驗(yàn)證碼的方法,對比 $_SESSION[an] 是否等于 $_POST[驗(yàn)證碼文本框ID]
* 可選配置:
* 1.驗(yàn)證碼類型:$an->ext_num_type=1; 值為1是小寫類型,2是大寫類型,3是數(shù)字類型
* 2.干擾點(diǎn):$an->ext_pixel = false; 值為false表示不添加干擾點(diǎn)
* 3.干擾線:$an->ext_line = false; 值為false表示不添加干擾線
* 4.Y軸隨機(jī):$an->ext_rand_y = false; 值為false表示不支持圖片Y軸隨機(jī)
* 5.圖片背景:改變 $red $green $blue 三個(gè)成員變量的值即可
**/
$an = new Authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //干擾點(diǎn)
$an->ext_line = false; //干擾線
$an->ext_rand_y= true; //Y軸隨機(jī)
$an->green = 238;
$an->create();
?>
您可能感興趣的文章:
- 個(gè)人寫的PHP驗(yàn)證碼生成類分享
- 一個(gè)漂亮的php驗(yàn)證碼類(分享)
- 一個(gè)經(jīng)典的PHP驗(yàn)證碼類分享
- 一個(gè)好用的PHP驗(yàn)證碼類實(shí)例分享
- 一個(gè)PHP驗(yàn)證碼類代碼分享(已封裝成類)
- 一個(gè)實(shí)用的php驗(yàn)證碼類
- PHP實(shí)現(xiàn)的封裝驗(yàn)證碼類詳解
- PHP使用GIFEncoder類生成的GIF動(dòng)態(tài)圖片驗(yàn)證碼
- 一個(gè)簡單安全的PHP驗(yàn)證碼類、PHP驗(yàn)證碼
- 分享一個(gè)漂亮的php驗(yàn)證碼類
- PHP驗(yàn)證碼類ValidateCode解析
- PHP code 驗(yàn)證碼生成類定義和簡單使用示例
相關(guān)文章
php實(shí)現(xiàn)比較兩個(gè)字符串日期大小的方法
這篇文章主要介紹了php實(shí)現(xiàn)比較兩個(gè)字符串日期大小的方法,涉及php日期操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05php 獲取可變函數(shù)參數(shù)的函數(shù)
php取得可變函數(shù)參數(shù),方便后面的控制。2009-08-08PHP利用遞歸函數(shù)實(shí)現(xiàn)無限級分類的方法
今天小編就為大家分享一篇關(guān)于PHP利用遞歸函數(shù)實(shí)現(xiàn)無限級分類的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03php實(shí)現(xiàn)獲取局域網(wǎng)所有用戶的電腦IP和主機(jī)名、及mac地址完整實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)獲取局域網(wǎng)所有用戶的電腦IP和主機(jī)名、及mac地址,非常實(shí)用,需要的朋友可以參考下2014-07-07深入理解PHP之?dāng)?shù)組(遍歷順序) Laruence原創(chuàng)
經(jīng)常會有人問我, PHP的數(shù)組, 如果用foreach來訪問, 遍歷的順序是固定的么? 以什么順序遍歷呢?下面看Laruence整理的2012-06-06phpexcel導(dǎo)出excel的顏色和網(wǎng)頁中的顏色顯示不一致
關(guān)于phpexcel導(dǎo)出顏色的一些問題,用phpexcel做導(dǎo)出的excel的顏色怎么和網(wǎng)頁中的顏色顯示不一致呢,接下來將詳細(xì)介紹解決方法2012-12-12