验证码程序PHP

验证码的程序

XML/HTML代码
  1. //字体文件   
  2. $font  = $other_path.'./include/system/luxisr.ttf';   
  3.   
  4.   
  5. $width = array_key_exists('width', $_GET) ? $_GET['width'] : 70;   
  6. $high  = array_key_exists('high' , $_GET) ? $_GET['high']  : 20;   
  7. $size  = array_key_exists('size' , $_GET) ? $_GET['size']  : 12;   
  8.   
  9. //生成验证码   
  10. $text = '';   
  11. for($i=0; $i<4; $i++){   
  12.     $text .rand(0,9);   
  13. }   
  14.   
  15.   
  16. session_start();   
  17. $_SESSION['check_code']=$text;   
  18.   
  19. $image = imagecreate($width, $high);   
  20. $whiteColor = imagecolorallocate($image, 255, 255, 255);   
  21. $blackColor = imagecolorallocate($image,   0,   0,   0);   
  22. $count = $width * $high / 8;   
  23.   
  24. for($i = 0; $i < $count; $i++){   
  25.     $randomColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));   
  26.     imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $high), $randomColor);   
  27. }   
  28.   
  29. $fontSize = imagettfbbox($size, 0, $font, $text);   
  30. $centerX = abs($fontSize[2] - $fontSize[0]);   
  31. $centerY = abs($fontSize[5] - $fontSize[3]);   
  32.   
  33. $x = ($width - $centerX) / 2;   
  34. $y = ($high  - $centerY) / 2 + $centerY;   
  35.   
  36. ///生成图片   
  37. imagettftext($image, $size, mt_rand(-5, +5), $x, $y, $blackColor, $font, $text);   
  38. imagerectangle($image, 0, 0, $width - 1, $high - 1, $blackColor);   
  39. imagepng($image);   
  40. imagedestroy($image);  

提交表单中的程序

 

XML/HTML代码
  1. session_start();   
  2. $vote_code=$_SESSION['check_code'];   
  3. $vote_check=(int)$code;   
  4.   
  5. if(empty($vote_code) || $vote_code!=$vote_check){   
  6.     echo '<script>alert("非法投票,请检查验证码!");</script>';   
  7.     echo '<script>history.go(-1);</script>';   
  8.     exit();   
  9. }  

HTML中包含验证码

 

XML/HTML代码
  1. <img src="checkcode.php?t=<?=time();?>" id="checkcode" align=absmiddle style="margin-left:16px;margin-bottom:5px;">  
  2.       
  3. <input type="text" name="code" id="code" value="" size=8  style="margin-left:16px;margin-bottom:3px;" />  

« 上一篇 | 下一篇 »

发表评论

评论内容 (必填):