In this article I'll try to explain how to create a captcha image and how to use it.
First you need to ensure PHP5's GD is installed. In ubuntu the command is:
sudo apt-get install php5-gd
or other systems yum install php5-gd (I wont include a WIN32 install, but google should provide something
First of all, we need to set the header, tell the browser we're showing a PNG image. (Even though we will display it with imagejpeg())
Start a session, and set the width and height, You can adjust the height and width if you wish.
$im is the image that we're creating and we'll be reffering to this alot throughout the script.
Gotta set the white and black colours. [Note: I didnt use other colours, because after talking with Carb0n when i first created the script, he noted that a 'bot' can easily convert it to black and white]
Then we create the session string called captcha and give it whatever is in $new_string. Fill the background with black (or you can replace it with $white)
Then we insert a whole range of random white pixels. (i tested this, any more and the letters are unreadable, but experiment, you might like it)
The next for loop inserts the letters at random points along the image itself.
the imagejpeg outputs the image to your browser. Now onto the usage!
This code inserts the image into the HTML itself, and outputs to the browser (so long as thefile is located at includes/image.php)
Practical use?
HTML Form:
Javascript: [Note, this is to change the value within the captcha image]
Again, please check that includes/image.php IS the location of your image.
PHP: [your_login_page.php]
Note, i did Omit what wasn't needed. Hopefully you can see what's going on. Please contact me at kingsley-muir@hotmail.com if you need any help with it :)
| Author Info | Comment |
|---|---|
| Specific Posted about 1 year ago. |
Very good tutorial on how to create and use a captcha, very detailed. I think it's good practice to tell the browser not to cache the captcha image. I do this by modifying the header: This little php snippet tells the browser that the content has expired by giving it a date from the past, and not to store in the browser's cache. |
| Jordan Posted about 1 year ago. |
Thanks for the comment Specific. Nice comment to, not caching the image could go well. |

