Posted By

iroybot on 02/20/09


Tagged

image img spam Captcha


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

jfherring
jamesming
BrianCoyDesign
iroybot


Captchas


 / Published in: PHP
 

  1. <?php
  2. // safe this to a separate file image.php
  3. $letters=substr(str_shuffle("ACDJKMNOPSTVWXYZ"),0,4);
  4. $image = imagecreate(40,16);
  5. imagecolorallocate($image, 255, 255, 255);
  6. imagecolorallocate($image, 0, 0, 0);
  7. imagecolorallocate($image, rand(50,175), rand(125,225), rand(125,225));
  8. imagecolorallocate($image, rand(125,225), rand(50,175), rand(125,225));
  9. imageline($image, 0, rand(9,14), 40, rand(2,7), 2);
  10. imagestring($image, 5, 3, 0, " " . $letters[0] . " " . $letters[1], 1);
  11. imageline($image, 0, rand(2,7), 40, rand(9,14), 3);
  12. imagestring($image, 5, 3, 0, $letters[2] . " " . $letters[3], 1);
  13. $_SESSION["md5"] = md5($letters[2] . $letters[0] . $letters[3] . $letters[1]);
  14. header('Content-type: image/png');
  15. imagepng($image);
  16.  
  17. // usage example:
  18. <img src="./image.php"/>
  19. <label>type in the code:</label>
  20. <input name="c" type="text"/>
  21.  
  22. // validation example:
  23. if (isset($_POST['c']) { $captcha = stripslashes(trim(htmlspecialchars($_POST['c'])));
  24. } else {
  25. $captcha = false;
  26. }
  27. if ( $captcha && isset($_SESSION["md5"])&&$_SESSION["md5"]==md5( strtoupper($captcha) ) ) {
  28. // handle POST vars here
  29. print "Thanks for your submission";
  30. }
  31. ?>

Report this snippet  

You need to login to post a comment.