Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,217 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,226 people online right now. Registration is fast and FREE... Join Now!





Generating image with random number (anti-bot)

This code generate image, with random generated number (weak and user-annoying anti-bot security :P)

Submitted By: k0b13r
Actions:
Rating:
Views: 7,338

Language: PHP

Last Modified: August 24, 2007

Snippet


  1. <?php
  2.  
  3. function anti_bot($lenght=4)
  4. {
  5. $number = "";
  6. for ($i = 1; $i <= $lenght; $i++)
  7. {
  8.      $number .= rand(0,9)."";
  9. }
  10.  
  11. $width = 11*$lenght;
  12. $height = 30;
  13.  
  14. $img = ImageCreate($width, $height);
  15. $background = imagecolorallocate($img,255,255,255);
  16. $color_black = imagecolorallocate($img,0,0,0);
  17. $color_grey = imagecolorallocate($img,169,169,169);
  18. imagerectangle($img,0, 0,$width-1,$height-1,$color_grey);
  19. imagestring($img, 5, $lenght, 7, $number, $color_black);
  20. imagepng($img);
  21. imagedestroy($img);
  22. }
  23.  
  24. anti_bot();
  25. ?>

Copy & Paste


Comments


rjolitz 2008-06-22 20:03:21

I tried this out. Code is easy to follow but this is the output I got:

CODE
‰PNG  &#65533;&#65533;&#65533; IHDR&#65533;&#65533;&#65533;,&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;*X4&#65533;&#65533;&#65533; PLTEÿÿÿ&#65533;&#65533;&#65533;©©©x7Ó^&#65533;&#65533;&#65533;`IDAT•½ÐÑ €0ÐÃô6À}pƒk¢û8SJk»€òõ>ààz 'F-ß›²‡ØìNZ#­âÔŒ´máé*2hÝ»[ œ .Ù>Û23´gö]&#65533;ÿ¸kòôÛëÇ(wúïÐa&#65533;&#65533;&#65533;&#65533;IEND®B`‚
In FireFox 3. Anyone else have this problem?

k0b13r 2008-06-23 12:31:13

You have to send header with content type: header("Content-type: image/png"); before imagepng($img);

balazj 2008-07-31 12:36:19

I have problem with this code. I have tag : Now instead normal page with image, i have new page with ONLY image that generates Your code. Any idea?


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month