PHP Captcha Image Verification

CAPTCHA is used on many website to ensure that the response is not generated by computer. The term “CAPTCHA” was based upon the word capture. It is a contrived acronym for Completely Automated Public Turing test to tell Computers and Humans Apart. A common type of CAPTCHA requires that the user type letters or digits from a distorted image that appears on the screen. (Wikipedia).

Generating Captcha Image Using PHP

PHP allows us to create and/or manipulate image using it’s GD Library. In this article, I will show you how to generate a simple captcha image and use it as a form validation. You can learn more about manipulating image using PHP’s GD Library from the official site.

<?php

/* PHP Captcha Image Generator by Arie Putranto
    http://arie.putranto.com/blog/php-script/php-captcha-image-verification.html/ */

session_start();

// Font configuration
$font_size = 16;
$font_file = 'cour.ttf';
$string_length = 5;

// Calculating image dimension based on font configuration
$img_height = $font_size * 2;
$img_width = ($string_length * $font_size) + $font_size;

// Define the code for showing on the image
$_SESSION['validation'] = $string = (isset($_SESSION['code'])) ? substr($_SESSION['code'],0,$string_length) : strtoupper(substr(md5(rand(00000,99999)),0,5));

// Generating the captcha image
$image = @imagecreatetruecolor($img_width, $img_height);

// Define image colors
$bg_color = @imagecolorallocate($image, 252, 252, 252);
$border_color = @imagecolorallocate($image, 200, 200, 200);
$string_color = @imagecolorallocate($image, 100, 100, 100);

// Drawing the image backgroung and borders
@imagefill($image,0,0,$bg_color);
@imageline($image,0,0,0,$img_height,$border_color);
@imageline($image,$img_width-1,0,$img_width-1,$img_height,$border_color);
@imageline($image,0,0,$img_width-1,0,$border_color);
@imageline($image,0,$img_height-1,$img_width-1,$img_height-1,$border_color);

// Write the verification code into the image
for($i=0;$i<$string_length;$i++) {
 @imagettftext($image,$font_size,0,($font_size/2)+2+($font_size*$i),$img_height-($img_height/3)+1,$string_color,$font_file,$string[$i]);
}

// Load the image into the browser
header("Expires: Sun, 1 Jan 2000 12:00:00 GMT");
header("Last-Modified: " . @gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: image/jpeg");

@imagejpeg($image, NULL, 100);
@imagedestroy($image);

?>

The generated CAPTCHA image will look like this:

Using the captcha image in a form validation

To do that, we need to add a few PHP line on the form and save the form as a PHP file. Check out the code below:

<?php 

session_start();

// Generating the verification code for the CAPTCHA image
$_SESSION['code'] = strtoupper(md5(rand(00000,99999)));

// Processing the form request
if(isset($_POST['captcha'])) {
 $posted_captcha = stripslashes($_POST['captcha']);
 $validation = $_SESSION['validation'];
 if($posted_captcha != $validation) print 'Invalid captcha verification';
 else print 'Captcha code is verified';
} else {
 print 'Insert the number below (<em>Case sensitive</em>)';
}

?>
<img src="./captcha.php" alt="" />

<form name="test" method="post" enctype="multipart/form-data" action="">
<input type="text" name="captcha" value="" />
<input type="submit" value="Submit" />
</form>

Save it as a PHP file. To use the above scripts, simply upload them to the same directory along with the font file. You might need to style the form a bit to match your web design.

12 comments on "PHP Captcha Image Verification"
Leave a Response