[PHP] 生成验证码 →→→→→进入此内容的聊天室

来自 , 2019-09-29, 写在 PHP, 查看 141 次.
URL http://www.code666.cn/view/868b7df9
  1. <?php
  2. session_start(); //MUST START SESSION
  3. $string_length = 4; //NUMBER OF CHARS TO DISPLAY
  4. $large_letters = array('m','w');
  5. $rand_string = '';
  6.  
  7. for ($i=0; $i<$string_length; $i++) {
  8.   //PICK A RANDOM LOWERCASE LETTER USING ASCII CODE
  9.   $rand_string .= chr(rand(97,122));
  10. }
  11.  
  12. //IMAGE VARIABLES
  13. //$width = 100;
  14. //$height = 36;
  15. $width = 60;
  16. $height = 25;
  17.  
  18.  
  19. //INIT IMAGE
  20. $img = imagecreatetruecolor($width, $height);
  21.  
  22. //ALLOCATE COLORS
  23. $black = imagecolorallocate($img, 0, 0, 0);
  24. $gray = imagecolorallocate($img, 110, 110, 110);
  25. $medgray = imagecolorallocate($img, 180, 180, 180);
  26. $lightgray = imagecolorallocate($img, 220, 220, 220);
  27. //FILL BACKGROUND
  28. imagefilledrectangle($img, 0, 0, $width, $height, $lightgray);
  29.  
  30. //ADD NOISE - DRAW background squares
  31. $square_count = 6;
  32. for ($i = 0; $i < 10; $i++) {
  33.   $cx = (int)rand(0, $width/2);
  34.   $cy = (int)rand(0, $height);
  35.   $h  = $cy + (int)rand(0, $height/5);
  36.   $w  = $cx + (int)rand($width/3, $width);
  37.   imagefilledrectangle($img, $cx, $cy, $w, $h, $medgray);
  38. }
  39.  
  40. //ADD NOISE - DRAW ELLIPSES
  41. $ellipse_count = 5;
  42. for ($i = 0; $i < $ellipse_count; $i++) {
  43.   $cx = (int)rand(-1*($width/2), $width + ($width/2));
  44.   $cy = (int)rand(-1*($height/2), $height + ($height/2));
  45.   $h  = (int)rand($height/2, 2*$height);
  46.   $w  = (int)rand($width/2, 2*$width);
  47.   imageellipse($img, $cx, $cy, $w, $h, $gray);
  48. }
  49.  
  50. //REPLACE THIS WITH THE FONT YOU UPLOAD
  51. $font = '../tiff/DejaVuSerifCondensed-Italic.ttf';
  52. //$font = 'monofont.ttf';
  53. $font_size = 16;
  54.  
  55. //CALC APPROX LOCATION - CUSTOMIZED FOR ABOVE FONT
  56. $y_value = ($height/2) + ($font_size/2);
  57. $x_value = 0;
  58.  
  59. //DRAW STRING USING TRUE TYPE FUNCTION
  60.  
  61. for ($i = 0; $i < $string_length; $i++) {
  62.   $chr = substr($rand_string, $i, 1);
  63.   $x_value += 3 * ($font_size/5);
  64.   imagettftext($img, $font_size, 0, $x_value, $y_value, $black, $font, $chr);
  65.   //check to see if larger than normal letters, if so add more horiz space
  66.   if (in_array($chr, $large_letters)) {
  67.     $x_value += 4;
  68.   }
  69. }
  70.  
  71. $auth_type = isset($_REQUEST['type'])?$_REQUEST['type']:'';
  72. if($auth_type=="login")
  73. {
  74.         $_SESSION['authcode_login']=$rand_string;
  75. }
  76. else
  77. {
  78.         //$_SESSION['encoded_captcha'] = md5($rand_string . 'my_secret_key');
  79.         $_SESSION['authcode']=$rand_string;
  80. }
  81.  
  82. //OUTPUT IMAGE HEADER AND SEND TO BROWSER
  83. header("Content-Type: image/png");
  84. imagepng($img);
  85. ?>

回复 "生成验证码"

这儿你可以回复上面这条便签

captcha