欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php写的一个验证码

程序员文章站 2024-02-07 21:43:16
...
  1. session_start();
  2. $image = imagecreatetruecolor(58,22);
  3. $color_Background = imagecolorallocate($image,255,255,255);
  4. imagefill($image,0,0,$color_Background);
  5. $key = array(
  6. ‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,
  7. ‘a’,'b’,'c’,'d’,'e’,'f’,'g’,'h’,'i’,'j’,'k’,'l’,'m’,'n’,'o’,'p’,'q’,'r’,’s’,'t’,'u’,'v’,'w’,'x’,'y’,'z’,
  8. ‘A’,'B’,'C’,'D’,'E’,'F’,'G’,'H’,'I’,'J’,'K’,'L’,'M’,'N’,'O’,'P’,'Q’,'R’,'S’,'T’,'U’,'V’,'W’,'X’,'Y’,'Z’);
  9. $string = null;
  10. $char_X = 6;
  11. $char_Y = 0;
  12. for($i=0;$i{
  13. $char_Y = mt_rand(0,5);
  14. $char = $key[mt_rand(0,61)];
  15. $string .= $char;
  16. $color_Char = imagecolorallocate($image,mt_rand(0,230),mt_rand(0,230),mt_rand(0,230));
  17. imagechar($image,5,$char_X,$char_Y,$char,$color_Char);
  18. $char_X = $char_X + mt_rand(8,15);
  19. }
  20. $line_X1 = 0;
  21. $line_Y1 = 0;
  22. $line_X2 = 0;
  23. $line_Y2 = 0;
  24. for($i=0;$i
  25. {
  26. $line_X1 = mt_rand(0,58);
  27. $line_Y1 = mt_rand(0,22);
  28. $line_X2 = mt_rand(0,58);
  29. $line_Y2 = mt_rand(0,22);
  30. $line_X1 = $line_X1;
  31. $line_Y1 = $line_Y1;
  32. $line_X2 = $line_X1 + mt_rand(1,8);
  33. $line_Y2 = $line_Y1 + mt_rand(1,8);
  34. $color_Line = imagecolorallocate($image,mt_rand(0,230),mt_rand(0,230),mt_rand(0,230));
  35. imageline($image,$line_X1,$line_Y1,$line_X2,$line_Y2,$color_Line);
  36. }
  37. $_SESSION['seccode'] = $string;
  38. @header(“Expires: -1″);
  39. @header(“Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0″, FALSE);
  40. @header(“Pragma: no-cache”);
  41. header(‘Content-Type: image/jpeg’);
  42. imagepng($image);
  43. imagedestroy($image);
  44. ?>
复制代码