Creating a CAPTCHA


It's a fairly long code, so I'll explain it afterwards.

PHP Code
  1. <?php
  2. session_start();
  3. $alpanumb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstufwxyz1234567890";
  4. $code = substr(str_shuffle($alpanumb), 0, 5);
  5. $num = "1230";
  6. $rand = substr(str_shuffle($num), 0, 2);
  7. $coded = base64_encode($code);
  8. $_SESSION['CAPTCHA'] = $code;
  9. $width = 118;
  10. $height = 27;
  11.  
  12. $im = imagecreatefrompng("./captcha.PNG");
  13. $black = imagecolorallocate($im, rand(1, 255), rand(1, 255), rand(1, 255));
  14.  
  15. // grid
  16. $grid_color = imagecolorallocate($im, 175, 0, 0);
  17. $number_to_loop = ceil($width / 20);
  18. for($i = 0; $i < $number_to_loop; $i++) {
  19. $x = ($i + 1) * 20;
  20. imageline($im, $x, 0, $x, $height, $grid_color);
  21. }
  22. $number_to_loop = ceil($height / 10);
  23. for($i = 0; $i < $number_to_loop; $i++) {
  24. $y = ($i + 1) * 10;
  25. imageline($im, 0, $y, $width, $y, $grid_color);
  26. }
  27.  
  28. // random lines
  29. $line_color = imagecolorallocate($im, 0, 0, 0);
  30. for($i = 0; $i < 30; $i++) {
  31. $rand_x_1 = rand(0, $width - 1);
  32. $rand_x_2 = rand(0, $width - 1);
  33. $rand_y_1 = rand(0, $height - 1);
  34. $rand_y_2 = rand(0, $height - 1);
  35. imageline($im, $rand_x_1, $rand_y_1, $rand_x_2, $rand_y_2, $line_color);
  36. }
  37.  
  38. imagestring($im, 5, $rand, 10, $code, $black);
  39. header("Content-type: image/png");
  40. header("no-cache");
  41. imagepng($im);
  42. echo "<BR><BR>$random1";
  43. ?>


Alrighty then, you can call that whatever you wish.

-----------------------HOW TO USE-----------------------

**PLEASE NOTE: You must create your own image or use the one below:

TutorialNinja Image

Just recolour it to your needs. To show the image, all you must do it on your page (not the php page) put this where you want it:

PHP Code
  1. <img src="captcha.php">


Just change captcha with the file name. Sometimes the font color is not readable (or at least for mine) so instead change the above image code to:

PHP Code
  1. <img src="../captcha.php" onclick="this.src='../captcha.php?time=' + (new Date()).getTime();">


-----------------------ABOUT-----------------------

Pretty self explanatory really, everything is in English. But, I'll explain the most important things.

$im, specifies the image, allowing modifications to happen.
$black, specifies the font colour, don't know why it's called black.

That's basically it. Nothing more too it. Hope this helps!
Diablosblizz's Avatar
Views:
2,808
Rating:
Posted on Sunday 28th September 2008 at 05:56 AM
Nathan
Nathan's Avatar
nope still dont work
Posted on Sunday 28th September 2008 at 05:46 AM
ShadowMage
ShadowMage's Avatar
remove session_start(); from the top? or maybe the echo at the bottom.
Posted on Sunday 28th September 2008 at 04:57 AM
Nathan
Nathan's Avatar
Warning: imagecreatefrompng(./usersystem/captcha.PNG) [function.imagecreatefrompng]: failed to open stream: No such file or directory in /home/rdlegend/public_html/usersystem/captcha.php on line 12

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 13

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 16

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 25

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 25

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 25

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 29

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imagestring(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 38

Warning: Cannot modify header information - headers already sent by (output started at /home/rdlegend/public_html/usersystem/captcha.php:12) in /home/rdlegend/public_html/usersystem/captcha.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /home/rdlegend/public_html/usersystem/captcha.php:12) in /home/rdlegend/public_html/usersystem/captcha.php on line 40

Warning: imagepng(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 41
Posted on Wednesday 9th April 2008 at 06:13 PM
new2old
new2old's Avatar
Or even better change

Code
<img src="captcha.php">


to

Code
<img src="captcha.PNG">


and

Code
<img src="../captcha.php" onclick="this.src='../captcha.php?time=' + (new Date()).getTime();">


to

Code
<img src="../captcha.PNG" onclick="this.src='../captcha.php?time=' + (new Date()).getTime();">


;)
Posted on Wednesday 9th April 2008 at 03:17 PM
DanielXP
DanielXP's Avatar
And the header

header("Content-type: image/png");
Posted on Wednesday 9th April 2008 at 02:29 PM
new2old
new2old's Avatar
PNG's dont for some hosts, change it to JPG using photoshop so you dont lose and picture quality

Then change


$im = imagecreatefrompng("./captcha.PNG");

to


$im = imagecreatefrompng("./captcha.jpg");
Posted on Monday 7th April 2008 at 02:06 PM
DanielXP
DanielXP's Avatar
Comments on the code?
Posted on Sunday 6th April 2008 at 09:07 PM
Diablosblizz
Diablosblizz's Avatar
Hmm, the image didn't show:

https://clan-ik.frih.net/captcha.PNG

Sorry.