/****** 产生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);
?> 

以上就是【php生成SessionID和图片校验码的思路和实现代码】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)
发表我的评论

最新评论

  1. 暂无评论