captcha.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. require_once(realpath(dirname(__FILE__)).'/config.php');
  3. require_once(PATH_PHP.'security.php');
  4. function generate_captcha(){
  5. $captcha = substr(md5(rand()),0,7);
  6. $_SESSION['captcha'] = $captcha;
  7. //Set the image width and height
  8. $width = 100;
  9. $height = 20;
  10. //Create the image resource
  11. $image = ImageCreate($width, $height);
  12. //We are making three colors, white, black and gray
  13. $white = ImageColorAllocate($image, 255, 255, 255);
  14. $black = ImageColorAllocate($image, 0, 0, 0);
  15. $grey = ImageColorAllocate($image, 204, 204, 204);
  16. //Make the background black
  17. ImageFill($image, 0, 0, $black);
  18. //Add randomly generated string in white to the image
  19. ImageString($image, 3, 30, 3, $captcha, $white);
  20. //Throw in some lines to make it a little bit harder for any bots to break
  21. ImageRectangle($image,0,0,$width-1,$height-1,$grey);
  22. imageline($image, 0, $height/2, $width, $height/2, $grey);
  23. imageline($image, $width/2, 0, $width/2, $height, $grey);
  24. //Tell the browser what kind of file is come in
  25. header("Content-Type: image/jpeg");
  26. //Output the newly created image in jpeg format
  27. ImageJpeg($image);
  28. //Free up resources
  29. ImageDestroy($image);
  30. exit();
  31. }
  32. function compare_captcha($captcha){
  33. return isset($_SESSION['captcha'])&&$captcha == $_SESSION['captcha'];
  34. }
  35. ?>