PHP CAPTCHA

Everybody who has a web site is well aware of the contact or comment form spamming problem.

This is a small script which generates a captcha with the option to refresh the captch without submitting the form
You can download a working example here, with the image.php, form and captch refresh javascript

The image.php file
<?php
if ($_GET['a']>=1){
session_start();
function generate_verification()
{
srand((double)microtime()*1000000);
$rand = rand(0,999999999); 
$thecode = substr(strtoupper(md5($rand)), 2, 5);
//replace zero and O letter (we dont to confuse users)
$thecode = str_replace("O", "A", $thecode);
$thecode = str_replace("0", "B", $thecode);
$_SESSION["thecode"] = $thecode;
}
generate_verification();
header("Content-type: image/jpg");
$width=60;
$height=15;
$image = imagecreate($width,$height);
//Prepare the colors (RGB values)
$background_color = imagecolorallocate ($image, 0, 0, 0);
//the color of the noise lines
$noise_color = imagecolorallocate($image, 213, 211, 211);
//the color of the noise dotes
$noise_color2 = imagecolorallocate($image, 85, 255, 127);
//the color of the code
$TxtColor = imagecolorallocate($image, 255, 255, 255);
//Fill the image with lines
//the smaller the divider (150) the more lines
for( $i=0; $i<($width*$height)/150; $i++ )
{
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
//now fill the image with green dotes
//the smaller the divider (5) the more dotes
for( $i=0; $i<($width*$height)/5; $i++ )
{
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color2);
}
//Now generate the image
imagestring($image,5,8,0,$_SESSION["thecode"],$TxtColor);
imagejpeg($image);
imagedestroy($image);
}
?>