php生成SessionID和圖片校驗(yàn)碼的思路和實(shí)現(xiàn)代碼
更新時(shí)間:2009年03月10日 09:22:55 投稿:mdxy-dxy
做一個(gè)后臺(tái)登陸需要用到校驗(yàn)碼,前臺(tái)的用戶跟蹤需要用到SessionID,當(dāng)然,默認(rèn)的PHP開啟了Session以后就有了一個(gè)SessionID,但是我需要自己的,并且能夠存儲(chǔ)進(jìn)數(shù)據(jù)庫(kù),那么我就嘗試了一下,構(gòu)造了以下的函數(shù)。
/****** 產(chǎn)生Session ID ******/
基本的思路: 是把當(dāng)前微秒的時(shí)間獲取, 然后產(chǎn)生以個(gè)隨機(jī)數(shù)字, 把隨機(jī)數(shù)字和當(dāng)前時(shí)間相加后加密一下, 最后再截取需要的長(zhǎng)度
/*
函數(shù)名稱:create_sess_id()
函數(shù)作用:產(chǎn)生以個(gè)隨機(jī)的會(huì)話ID
參 數(shù):$len: 需要會(huì)話字符串的長(zhǎng)度,默認(rèn)為32位,不要低于16位
返 回 值:返回會(huì)話ID
函數(shù)作者:heiyeluren
*/
function create_sess_id($len=32)
{
// 校驗(yàn)提交的長(zhǎng)度是否合法
if( !is_numeric($len) || ($len>32) || ($len<16)) { return; }
// 獲取當(dāng)前時(shí)間的微秒
list($u, $s) = explode(' ', microtime());
$time = (float)$u + (float)$s;
// 產(chǎn)生一個(gè)隨機(jī)數(shù)
$rand_num = rand(100000, 999999);
$rand_num = rand($rand_num, $time);
mt_srand($rand_num);
$rand_num = mt_rand();
// 產(chǎn)生SessionID
$sess_id = md5( md5($time). md5($rand_num) );
// 截取指定需要長(zhǎng)度的SessionID
$sess_id = substr($sess_id, 0, $len);
return $sess_id;
}
/****** 產(chǎn)生校驗(yàn)碼 ******/
思路: 這個(gè)思路比較簡(jiǎn)單,因?yàn)榭紤]獨(dú)一無(wú)二和隨機(jī)性,我們的校驗(yàn)碼就Session ID里面截取一段字符串就可以了,因?yàn)槲覀兊腟essionID是充分考慮了獨(dú)一無(wú)二的。
/*
函數(shù)名稱:create_check_code()
函數(shù)作用:產(chǎn)生以個(gè)隨機(jī)的校驗(yàn)碼
參 數(shù):$len: 需要校驗(yàn)碼的長(zhǎng)度, 請(qǐng)不要長(zhǎng)于16位,缺省為4位
返 回 值:返回指定長(zhǎng)度的校驗(yàn)碼
函數(shù)作者:heiyeluren
*/
function create_check_code($len=4)
{
if ( !is_numeric($len) || ($len>6) || ($len<1)) { return; }
$check_code = substr(create_sess_id(), 16, $len );
return strtoupper($check_code);
}
/****** 生成校驗(yàn)碼的圖片 ******/
這個(gè)就是一些比較簡(jiǎn)單的PHP圖像編程的東西了,我作的圖片和簡(jiǎn)單。
/*
函數(shù)名稱:create_check_image()
函數(shù)作用:產(chǎn)生一個(gè)校驗(yàn)碼的圖片
參 數(shù):$check_code: 校驗(yàn)碼字符串,一般由create_check_code()函數(shù)來獲得
返 回 值:返回該圖片
函數(shù)作者:heiyeluren
*/
function create_check_image( $check_code )
{
// 產(chǎn)生一個(gè)圖片
$im = imagecreate(65,22);
$black = ImageColorAllocate($im, 0,0,0); // 背景顏色
$white = ImageColorAllocate($im, 255,255,255); // 前景顏色
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
// 將四位整數(shù)驗(yàn)證碼繪入圖片
imagestring($im, 5, 8, 3, $check_code, $white);
// 加入干擾象素
for($i=0;$i<200;$i++)
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
// 輸出圖像
Header("Content-type: image/PNG");
ImagePNG($im);
ImageDestroy($im);
}
這里我們要注意,引用create_check_image()函數(shù)的時(shí)候,必須在一個(gè)單獨(dú)的文件里,因?yàn)檩敵鑫募^的時(shí)候輸出的格式是圖像格式,夾雜其他內(nèi)容,會(huì)導(dǎo)致圖片無(wú)法顯示。另外,圖片成生函數(shù),你是可以更改的,比如你想改顏色,那么你就把前景色和背景色的生成位置換一下,那么顏色就不一樣了,同時(shí)也要把校驗(yàn)碼的顏色換了,不然背景和校驗(yàn)碼都是黑色就顯示不出來了。
PHP校驗(yàn)碼生成--備忘
<?php
session_start();//保存生成值,以與用戶輸入比較
//-------------------------------------------------------------------------
$img_w = 80;// 設(shè)置圖片寬
$img_h = 20;// 設(shè)置圖片高
$pixel_num = 200;//點(diǎn)越多干擾越大
$is_set_line = true;// 啟用干擾線
$pixel_mode = 2;// 干擾點(diǎn)模式,1,同色;2,雜色
//-------------------------------------------------------------------------
// 隨機(jī)數(shù)產(chǎn)生器
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
mt_srand(make_seed());//4.2.0以下版本適用
$authnum = mt_rand(100, 99999);
// 加入session
$_SESSION['verifycode']=$authnum;
//echo $authnum;
//生成驗(yàn)證碼圖片
Header("Content-type: image/PNG");
$im = imagecreatetruecolor($img_w, $img_h);
$bg_color = ImageColorAllocate($im, mt_rand(250,255),mt_rand(250,255),mt_rand(250,255));
// 繪制背景
imagefill($im,0,0,$bg_color);
$total_width = 0;
$word_info = array();
// 循環(huán),獲取文字信息
$word_length = strlen($authnum);
for($ii=0; $ii<$word_length; $ii++)
{
$word_space = mt_rand(1,5);
$font = rand(3,5);
mt_rand(1,9)%2 == 0?$top = 1:$top = 3;
$word_info[$ii]['char'] = substr($authnum,$ii,1);
$word_info[$ii]['font'] = $font;
$word_info[$ii]['offset'] = $top;
if($ii == 0)
{
$word_info[$ii]['width'] = 0;
}
$word_info[$ii]['width'] = imageFontWidth($font)+$word_space;
$word_info[$ii]['height'] = imageFontHeight($font);
$word_info[$ii]['color'] = imageColorAllocate($im, mt_rand(0,50),mt_rand(0,150),mt_rand(0,200));
// 文字總寬度
$total_width += $word_info[$ii]['width'];
// 取第一個(gè)字體的高度
if($ii == 0)
{
$total_height = imagefontHeight($font);
}
}
// 計(jì)算偏移
$offset_x = floor(($img_w - $total_width)/2);
$offset_y = floor(($img_h - $total_height)/2);
// 填充驗(yàn)證碼
$wid = 0;
$i = 0;
foreach($word_info as $key=>$val)
{
if($i>0)
{
$wid += $val['width'];
}
imagestring($im, $val['font'], $offset_x + $wid, $val['offset'] + $offset_y, $val['char'], $val['color']);
$i++;
}
switch($pixel_mode)
{
case 1:
$pixel_color = ImageColorAllocate($im,
mt_rand(50,255),
mt_rand(50,255),
mt_rand(50,255));
// 干擾象素
for($i=0;$i<$pixel_num;$i++)
{
imagesetpixel($im, mt_rand()%$img_w , mt_rand()%$img_h , $pixel_color);
}
break;
case '2':
// 干擾象素
for ($i=0;$i<=128;$i++)
{
$pixel_color = imagecolorallocate ($im, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($im,mt_rand(2,128),mt_rand(2,38),$pixel_color);
}
break;
default:
$pixel_color = ImageColorAllocate($im,
mt_rand(50,255),
mt_rand(50,255),
mt_rand(50,255));
// 干擾象素
for($i=0;$i<$pixel_num;$i++)
{
imagesetpixel($im, mt_rand()%$img_w , mt_rand()%$img_h , $pixel_color);
}
break;
}
ImagePNG($im);
?>
相關(guān)文章
用PHP將網(wǎng)址字符串轉(zhuǎn)換成超鏈接(網(wǎng)址或email)
該函數(shù)將 URL 和 E-mail 地址字符串轉(zhuǎn)換為可點(diǎn)擊的超級(jí)鏈接。2010-05-05
PHP 5.3和PHP 5.4出現(xiàn)FastCGI Error解決方法
這篇文章主要介紹了PHP 5.3和PHP 5.4出現(xiàn)FastCGI Error解決方法,需要的朋友可以參考下2015-02-02
PHP實(shí)現(xiàn)的服務(wù)器一致性hash分布算法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的服務(wù)器一致性hash分布算法,結(jié)合實(shí)例形式分析了php一致性hash分布算法類的具體定義與相關(guān)使用技巧,需要的朋友可以參考下2018-08-08
PHP實(shí)現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長(zhǎng)度一半的數(shù)字算法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長(zhǎng)度一半的數(shù)字算法,涉及php數(shù)組的遍歷、統(tǒng)計(jì)、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
php中的Base62類(適用于數(shù)值轉(zhuǎn)字符串)
以下是對(duì)php中Base62類的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08

