/****** 產生Session ID ******/
基本的思路: 是把當前微秒的時間獲取, 然後產生以個隨機數字, 把隨機數字和當前時間相加後加密一下, 最後再截取需要的長度
/*
函數名稱:create_sess_id()
函數作用:產生以個隨機的會話ID
參 數:$len: 需要會話字符串的長度,默認為32位,不要低於16位
返 回 值:返回會話ID
函數作者:heiyeluren
*/
function create_sess_id($len=32)
{
// 校驗提交的長度是否合法
if( !is_numeric($len) || ($len>32) || ($len<16)) { return; }
// 獲取當前時間的微秒
list($u, $s) = explode(' ', microtime());
$time = (float)$u + (float)$s;
// 產生一個隨機數
$rand_num = rand(100000, 999999);
$rand_num = rand($rand_num, $time);
mt_srand($rand_num);
$rand_num = mt_rand();
// 產生SessionID
$sess_id = md5( md5($time). md5($rand_num) );
// 截取指定需要長度的SessionID
$sess_id = substr($sess_id, 0, $len);
return $sess_id;
}
/****** 產生校驗碼 ******/
思路: 這個思路比較簡單,因為考慮獨一無二和隨機性,我們的校驗碼就Session ID裡面截取一段字符串就可以了,因為我們的SessionID是充分考慮了獨一無二的。
/*
函數名稱:create_check_code()
函數作用:產生以個隨機的校驗碼
參 數:$len: 需要校驗碼的長度, 請不要長於16位,缺省為4位
返 回 值:返回指定長度的校驗碼
函數作者: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);
}
/****** 生成校驗碼的圖片 ******/
這個就是一些比較簡單的PHP圖像編程的東西了,我作的圖片和簡單。
/*
函數名稱:create_check_image()
函數作用:產生一個校驗碼的圖片
參 數:$check_code: 校驗碼字符串,一般由create_check_code()函數來獲得
返 回 值:返回該圖片
函數作者:heiyeluren
*/
function create_check_image( $check_code )
{
// 產生一個圖片
$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);
// 將四位整數驗證碼繪入圖片
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()函數的時候,必須在一個單獨的文件裡,因為輸出文件頭的時候輸出的格式是圖像格式,夾雜其他內容,會導致圖片無法顯示。另外,圖片成生函數,你是可以更改的,比如你想改顏色,那麼你就把前景色和背景色的生成位置換一下,那麼顏色就不一樣了,同時也要把校驗碼的顏色換了,不然背景和校驗碼都是黑色就顯示不出來了。
PHP校驗碼生成--備忘
<?php
session_start();//保存生成值,以與用戶輸入比較
//-------------------------------------------------------------------------
$img_w = 80;// 設置圖片寬
$img_h = 20;// 設置圖片高
$pixel_num = 200;//點越多干擾越大
$is_set_line = true;// 啟用干擾線
$pixel_mode = 2;// 干擾點模式,1,同色;2,雜色
//-------------------------------------------------------------------------
// 隨機數產生器
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;
//生成驗證碼圖片
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();
// 循環,獲取文字信息
$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'];
// 取第一個字體的高度
if($ii == 0)
{
$total_height = imagefontHeight($font);
}
}
// 計算偏移
$offset_x = floor(($img_w - $total_width)/2);
$offset_y = floor(($img_h - $total_height)/2);
// 填充驗證碼
$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);
?>