Tuesday, September 8, 2009

PHP Captcha Security Images

This script generates images (known as "Captcha's") which contain security codes used for protecting a form from spam bots. By encoding a 'password' inside an image and asking the user to re-enter what they see you can verify the user is a human and not automated software submitting your form. Why not try out the following form with valid and invalid codes to see how it works.



Copy and paste the above code and save it on your webserver as CaptchaSecurityImages.php. You will also need to place a copy of the "Monofont" font in the same directory as the CaptchaSecurityImages.php file. (Alternatively you can replace the line var $font = 'monofont.ttf'; with the name of whatever font you want to use)

You can download the captcha zip which contains all the files needed to implement the script including the required font.

Place the following code on your form. This will generate an image with a random string of characters along with the text field where the user will retype the code.



You can also specify certain options for the image by passing them as variables to CaptchaSecurityImages.php. The options available are the width and height of the image and the number of characters



Place the following in the code where the form is submitted to. This code will check what the user has typed matches the code in the image.


Optional Extras

You may wish to change the colour of the captcha image, this can be done by editing the background_colour, text_colour and noise_colour variables. The imagecolorallocate() function constucts a colour from the given RGB (red, green and blue) values, each of these is a number between 0-255. Another idea you might want to try is using the mt_rand function to randomize the colour each time a captcha is generated.

0 comments:

Post a Comment

 

Web Tips Copyright © 2009 WoodMag is Designed by Ipietoon for Free Blogger Template