Advertisement
opsftw

PHP: Basic captcha

Sep 18th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. NOTE: use this to generate the image, and check the user input against {$_SESSION['captcha']}
  2.  
  3. <?php
  4. # start the session
  5. session_start();
  6.  
  7. # set headers
  8. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  9. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  10. header("Cache-Control: no-store, no-cache, must-revalidate");
  11. header("Cache-Control: post-check=0, pre-check=0", false);
  12. header("Pragma: no-cache");
  13. header( "Content-type: image/png" );
  14.  
  15. # create the captia text
  16. $text = substr(md5(rand(0,9999999)),2,14);
  17. $_SESSION['captcha'] = $text;
  18.  
  19. # generate the image
  20. $my_img = imagecreate( 200, 80 );
  21. $background = imagecolorallocate( $my_img, 0, 0, 255 );
  22. $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
  23. $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
  24. $x=30;
  25. for($i=0;$i<=13;$i++){
  26.     $text_colour = imagecolorallocate( $my_img, rand(140,255), rand(0,255), rand(0,255));
  27.     imagestring($my_img,rand(3,7),$x,25,$text[$i],$text_colour);
  28.     $x+=10;
  29. }
  30. imagesetthickness ( $my_img, 5 );
  31. imageline( $my_img, 30, 45, 165, 45, $line_colour );
  32. imagepng( $my_img );
  33. imagecolordeallocate( $line_color );
  34. imagecolordeallocate( $text_color );
  35. imagecolordeallocate( $background );
  36. imagedestroy( $my_img );
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement