Captcha Image

A captcha™ is a human readable image to prevent "spambots" completing your online forms.

The term was first coined by researchers at the Carnegie Mellon University in 2000. As time has passed a whole "industry" has developed creating more sophisticated methods to obscure the image and on the other side creating more sophisticated methods of reading them automatically.

The software here should offer sufficient protection for the average user although it won't stand up to an attack from a more sophisticated spambot.

The code is in three sections

You also need a transparent gif with a line though it to help obscure the text.

Try it out:

arial.ttf
captcha
AirCutLight.ttf
captcha
AirbagStreet.ttf
captcha
OmegaSentry.ttf
captcha


The code uses session variables. It is important that the code to start the session is the first thing sent out by the page so ensure there are no spaces before the opening <?php. To generate my captcha code I use an array of three letter words and select two of these at random.

<?php
session_start();
$captcha=array(
"aah",
"aal",
"aas",
"aba", ................... "zip",
"zit",
"zoa",
"zoo");
if ($_POST['Submit']){
if ($_POST['captcha']==$_SESSION['captchacode']){
echo "You got it right<br><br>";
}
else {
echo "You got it wrong<br><br>";
}
}
$_SESSION['captchacode']=$captcha[rand(0,971)] .$captcha[rand(0,971)];
?>

The code to generate the image. Save this is as captchimage.php.

<?php
session_start();
header ("Content-type: image/png");
$fonts=array ("arial.ttf","AirCutLight.ttf", "AirbagStreet.ttf","OmegaSentry.ttf");
$font=$fonts[$_GET['font']];
$text=$_SESSION['captchacode'];
$img = imagecreatefromgif('back.gif');
$black = imagecolorallocate($img, 0, 0, 0);
imagettftext($img, 20, 0, 10, 20, $black, $font, $text);
imagepng($img);
imagedestroy($img);
?>

And finally the imput form. This can be saved in the same php page as the first section of the code.

arial.ttf<br>
<image src="captchaimage.php?font=0"><br>
AirCutLight.ttf<br>
<image src="captchaimage.php?font=1"><br>
AirbagStreet.ttf<br>
<image src="captchaimage.php?font=2"><br>
OmegaSentry.ttf<br>
<image src="captchaimage.php?font=3"><br>
<form method="post">
<input name="captcha" type="text" size="10">
<input name="Submit" type="submit" value="Submit">
</form>

Download

Other fonts are available at webpagepublicity.com (external link)