请输入关键字

Posted on 2008-08-15 10:32  flourish  阅读(1450)  评论(0)    收藏  举报
safeCode.php文件中的代码如下所示:

  1. <?   
  2. if(isset($_REQUEST["rand"]) && !emptyempty($_REQUEST["rand"])){   
  3.   $randStr = $_REQUEST["rand"];   
  4. }else {   
  5.   $randStr = "1234";   
  6. }   
  7. $type = 'gif';    
  8. $width= 40;    
  9. $height= 18;    
  10. header("Content-type: image/".$type);    
  11. srand((double)microtime()*1000000);    
  12. $randval = $randStr;    
  13. if($type!='gif' && function_exists('imagecreatetruecolor')){    
  14.   $im = @imagecreatetruecolor($width,$height);    
  15. }else{    
  16.   $im = @imagecreate($width,$height);    
  17. }    
  18. $r = Array(225,211,255,223);    
  19. $g = Array(225,236,237,215);    
  20. $b = Array(225,236,166,125);    
  21.   
  22. $key = rand(0,3);    
  23.   
  24. $backColor = ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)    
  25. $borderColor = ImageColorAllocate($im, 0, 0, 0);//边框色    
  26. $pointColor = ImageColorAllocate($im, 255, 170, 255);//点颜色    
  27.   
  28. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置    
  29. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置    
  30. $stringColor = ImageColorAllocate($im, 255,51,153);    
  31.   
  32. for($i=0;$i<=100;$i++){    
  33.   $pointX = rand(2,$width-2);    
  34.   $pointY = rand(2,$height-2);    
  35.   @imagesetpixel($im$pointX$pointY$pointColor);    
  36. }    
  37.   
  38. @imagestring($im, 3, 5, 1, $randval$stringColor);    
  39. $ImageFun='Image'.$type;    
  40. $ImageFun($im);    
  41. @ImageDestroy($im);   
  42. ?>  


function.php中产生随机字符的代码如下所示:

  1. <?   
  2. //产生随机字符串    
  3. function randStr($len=6,$format='ALL') {    
  4.   switch($format) {    
  5.     case 'ALL':    
  6.       $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';    
  7.       break;    
  8.     case 'CHAR':    
  9.       $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';    
  10.       break;    
  11.     case 'NUMBER':    
  12.       $chars='0123456789';    
  13.       break;    
  14.     default :    
  15.       $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';    
  16.     break;    
  17.   }    
  18.   $string="";    
  19.   while(strlen($string)<$len)    
  20.     $string.=substr($chars,(mt_rand()%strlen($chars)),1);    
  21.   return $string;    
  22. }   
  23. ?>  


如何使用:
在需要用到验证码的地方加下面的代码,你就会看到生成的验证码图片了
  1. <img src="safeCode.php?rand=123456">  

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3