Advertisement
rdsedmundo

captcha

Oct 28th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. <?
  2.  
  3. session_start();
  4.  
  5. $strings = '123456789';
  6. $i = 0;
  7. $characters = 6;
  8. $code = '';
  9. while ($i < $characters)
  10. {
  11.     $code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
  12.     $i++;
  13. }
  14.  
  15. $_SESSION['captcha'] = $code;
  16.  
  17. //generate image
  18. $im = imagecreatetruecolor(200, 50);
  19.  
  20. $foreground = imagecolorallocate($im, 0, 0, 0);
  21. $shadow = imagecolorallocate($im, 173, 172, 168);
  22.  
  23. $background = imagecolorallocate($im, 233, 239, 244);
  24.  
  25. imagefilledrectangle($im, 0, 0, 200, 200, $background);
  26.  
  27. // use your own font!
  28. $font = realpath(dirname(__FILE__)).'/arial.ttf';
  29.  
  30. //draw text:
  31. imagettftext($im, 35, 0, 9, 38, $shadow, $font, $code);
  32. imagettftext($im, 35, 0, 2, 42, $foreground, $font, $code);
  33.  
  34. // prevent client side  caching
  35. header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
  36. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  37. header("Cache-Control: no-store, no-cache, must-revalidate");
  38. header("Cache-Control: post-check=0, pre-check=0", false);
  39. header("Pragma: no-cache");
  40.  
  41. //send image to browser
  42. header ("Content-type: image/png");
  43. imagepng($im);
  44. imagedestroy($im);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement