首页 > 语言 > PHP > 正文

php加入干扰的生成验证码代码

2024-09-04 11:44:19
字体:大 中 小
来源:转载
供稿:网友

本款php生成验证码代码原理生成随机数-->创建图片-->随机数写进图片-->保存在session中,看一下流程验证码图片生成 通知浏览器将要输出PNG图片,准备好随机数发生器种子 srand((double)microtime()*1000000); 将四位整数验证码绘入图片.

php加入干扰的生成验证码代码如下:

  1. /*===================== 
  2. 产生随机字符串函数 
  3. =====================*/ 
  4. function random($length) { 
  5.  $hash = ''; 
  6.  $chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz'; 
  7.  $max = strlen($chars) - 1; 
  8.  mt_srand((double)microtime() * 1000000); 
  9.  for($i = 0; $i < $length; $i++) { 
  10.   $hash .= $chars[mt_rand(0, $max)]; 
  11.  } 
  12.  return $hash; 
  13. } 
  14.  
  15. //验证码图片生成 
  16.  
  17.  session_start(); 
  18.     //通知浏览器将要输出png图片  
  19.     header("content-type: image/png");  
  20.     //准备好随机数发生器种子   
  21.     //srand((double)microtime()*1000000);  
  22.     //准备图片的相关参数    
  23.     $im = imagecreate(62,22);  
  24.     $black = imagecolorallocate($im, 0,0,0);  //rgb黑色标识符  
  25.     $white = imagecolorallocate($im, 255,255,255); //rgb白色标识符  
  26.     $gray = imagecolorallocate($im, 179,183,185); //rgb灰色标识符  
  27.     //开始作图      
  28.     imagefill($im,0,0,$gray);  
  29.     //while(($randval=rand()%100000)<10000);{  
  30.         //$_session["check_code"] = $randval;  
  31.         //将四位整数验证码绘入图片 
  32.   $randval=random(4); 
  33.   $_session["check_code"]=$randval; 
  34.         imagestring($im, 5, 10, 3, $randval, $white);  
  35.     //}  
  36.     //加入干扰象素     
  37.     for($i=0;$i<150;$i++){  
  38.         $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));  
  39.         imagesetpixel($im, rand()%70 , rand()%30 , $white);  
  40.     } //开源代码Vevb.com 
  41.     //输出验证图片  
  42.     imagepng($im);  
  43.     //销毁图像标识符  
  44.     imagedestroy($im); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表