vercode.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * 生成4位随机字符的表单验证码
  4. *
  5. * @createtime 2018/03/01
  6. * @author 空竹(借鉴了网上的代码)
  7. * @copyright 芝麻开发(http://www.zhimawork.com)
  8. */
  9. session_start();
  10. $x_size = 60;
  11. $y_size = 20;
  12. $seccode = code_random(4);
  13. $_SESSION['ZhimaPHP_imgcode'] = $seccode;
  14. if(function_exists('imagecreate') && function_exists('imagecolorallocate') &&
  15. function_exists('imagesetpixel') && function_exists('imageString') && function_exists('imagedestroy') && function_exists('imagefilledrectangle') && function_exists('imagerectangle') && (function_exists('imagepng') || function_exists('imagejpeg'))) {
  16. $img = imagecreate($x_size, $y_size);
  17. $bgcolor = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
  18. $border = imagecolorallocate($img, 0xCC, 0xCC, 0xCC);
  19. imagefilledrectangle($img, 0, 0, $x_size - 1, $y_size - 1, $bgcolor);
  20. imagerectangle($img, 0, 0, $x_size - 1, $y_size - 1, $border);
  21. for($i=1; $i<=20;$i++){
  22. $dot = imagecolorallocate($img,mt_rand(50,255),mt_rand(50,255),mt_rand(50,255));
  23. imagesetpixel($img,mt_rand(2,$x_size-2), mt_rand(2,$y_size-2),$dot);
  24. }
  25. imageline($img,mt_rand(1,5),mt_rand(1,$y_size),mt_rand(5,$x_size/2),mt_rand(1, $y_size),$dot);
  26. imageline($img,mt_rand($x_size/2,($x_size/2)+5),mt_rand(1, $y_size),mt_rand(($x_size/2+5),$x_size),mt_rand(1, $y_size),$dot);
  27. for($i = 0; $i < strlen($seccode); $i++) {
  28. imageString($img, mt_rand(4,5), $i * $x_size / 4 + mt_rand(2, 5), mt_rand(1, 6), $seccode[$i], imagecolorallocate($img, mt_rand(50, 255), mt_rand(0, 120), mt_rand(50, 255)));
  29. }
  30. header("Pragma:no-cache");
  31. header("Cache-control:no-cache");
  32. if(function_exists('imagepng') && imagepng($img)) {
  33. header("Content-type: image/png");
  34. imagepng($img);
  35. } else {
  36. header("Content-type: image/jpeg");
  37. imagejpeg($img);
  38. }
  39. imagedestroy($img);
  40. exit;
  41. } else {
  42. header("Pragma:no-cache");
  43. header("Cache-control:no-cache");
  44. header("ContentType: Image/BMP");
  45. $Color[0] = chr(0).chr(0).chr(0);
  46. $Color[1] = chr(255).chr(255).chr(255);
  47. $_Num[0] = "1110000111110111101111011110111101001011110100101111010010111101001011110111101111011110111110000111";
  48. $_Num[1] = "1111011111110001111111110111111111011111111101111111110111111111011111111101111111110111111100000111";
  49. $_Num[2] = "1110000111110111101111011110111111111011111111011111111011111111011111111011111111011110111100000011";
  50. $_Num[3] = "1110000111110111101111011110111111110111111100111111111101111111111011110111101111011110111110000111";
  51. $_Num[4] = "1111101111111110111111110011111110101111110110111111011011111100000011111110111111111011111111000011";
  52. $_Num[5] = "1100000011110111111111011111111101000111110011101111111110111111111011110111101111011110111110000111";
  53. $_Num[6] = "1111000111111011101111011111111101111111110100011111001110111101111011110111101111011110111110000111";
  54. $_Num[7] = "1100000011110111011111011101111111101111111110111111110111111111011111111101111111110111111111011111";
  55. $_Num[8] = "1110000111110111101111011110111101111011111000011111101101111101111011110111101111011110111110000111";
  56. $_Num[9] = "1110001111110111011111011110111101111011110111001111100010111111111011111111101111011101111110001111";
  57. echo chr(66).chr(77).chr(230).chr(4).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(54).chr(0).chr(0).chr(0).chr(40).chr(0).chr(0).chr(0).chr(40).chr(0).chr(0).chr(0).chr(10).chr(0).chr(0).chr(0).chr(1).chr(0);
  58. echo chr(24).chr(0).chr(0).chr(0).chr(0).chr(0).chr(176).chr(4).chr(0).chr(0).chr(18).chr(11).chr(0).chr(0).chr(18).chr(11).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0);
  59. for ($i=9;$i>=0;$i--){
  60. for ($j=0;$j<=3;$j++){
  61. for ($k=1;$k<=10;$k++){
  62. if(mt_rand(0,7)<1){
  63. echo $Color[mt_rand(0,1)];
  64. }else{
  65. echo $Color[substr($_Num[$seccode[$j]], $i * 10 + $k, 1)];
  66. }
  67. }
  68. }
  69. }
  70. exit;
  71. }
  72. function code_random($length) {
  73. PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  74. $hash = '';
  75. $chars = 'abcdefhkmnpqrstuvwxyz123456789';
  76. $max = strlen($chars) - 1;
  77. for($i = 0; $i < $length; $i++) {
  78. $hash .= $chars[mt_rand(0, $max)];
  79. }
  80. return $hash;
  81. }
  82. ?>