Simple Captcha using PHP with GD library

Captcha can come in handy when fighting against spam and bot attacks. It generates random images and asks the user to re-enter the characters it sees in order to verify whether user is a human or not. We will develop a simple captcha script in PHP by using GD library. The only requirement is that you should have the GD library installed. It gives you the ability to create and manipulate image files in different formats in PHP.

The first thing we need to do is that search for a TrueType font on the internet. There are several sites which give TrueType fonts for free. Download a font of your choice from such a website. The TrueType font file will have .ttf as its extension. Place this file in the same folder in which you will place your PHP script. In this example we will use Turn Tablz font.

Turn Tablz font


Now create a new file named captcha.php and add the following code in it -



/*Create a 220x35 image*/
$im = imagecreatetruecolor(220, 35);

/*Color code for orange*/
$orange = imagecolorallocate($im, 0xFF, 0x8c, 0x00);

/*Color code for white*/
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

/*Generate a random string using md5*/ 
$md5_hash = md5(rand(0,999)); 

/*Trim the string down to 6 characters*/ 
$captcha_code = substr($md5_hash, 15, 6); 

/*Store the value of the generated captcha code in session*/
$_SESSION['captcha'] = $captcha_code; 

/* Set the background as orange */
imagefilledrectangle($im, 0, 0, 220, 35, $orange);

/*Path where TTF font file is present*/
$font_file = './TURNBB__.TTF';

/* Draw our randomly generated code*/
imagefttext($im, 30, 0, 5, 30, $white, $font_file, $captcha_code);

/* Output the image to the browser*/


Here we first create a new image of the specified size - 200x35 by using imagecreatetruecolor(). Next we use imagecolorallocate() function to create resource identifiers for the colors we need i.e. orange and white. We generate a random string by using md5() function and then trim it to a string having 6 characters. We also store this string/code in a PHP session variable so that it can be accessed later on for verification. After this we create a rectangle and fill it with orange color by using imagefilledrectangle(). Then we write our code over this rectangle by using imagefttext(). Finally we send the image to the browser by setting the appropriate header and using imagepng() function.

We can now use this script in evaluating any form. Create another file named form.php and add the following code to it.


<?php session_start(); ?>

<form method="post" action="">
<b>CAPTCHA check</b> (six letter code):<br /><br />
    <img src="captcha.php" alt="captcha image"><br />
    <input type="text" name="captcha" size="20" maxlength="6">


/*if captcha code is posted*/
/*CAPTCHA is valid. You can process the form now.*/
echo 'CAPTCHA is valid. You can process the form now.';
/*Invalid CAPTCHA*/
echo 'Invalid CAPTCHA. Try again!';

As you see we have given the source of captcha image as captcha.php. When a user accesses this form, automatically a random captcha image is generated and displayed. Also the corresponding code has been stored in the session variable $_SESSION['captcha']. When the re-enters the code and submits it we can check the entered value of the code against the stored session value and then process the form only if both match. Check out the working demo of the code below -