Generate and Implement Simple Captcha Code In PHP


To avoid automatic form submission some kind of verification should be used. CAPTCHA code submission is a best way to stop spammers. A CAPTCHA has a challenge-response test used in web form to ensure that the response is generated by a human being. In this article you will learn how to generate and implement simple captcha code in php. Most widely used captcha for verification of human being is image captcha.

To generate an image captcha you need to dynamically create an image with a random string on it. Start a session and store your random generated string of 5 chars as security code.

// captcha.php
session_start();
	
//Let's generate a random string using md5
$md5_hash = md5(rand(0,999));
	
//We don't need a 32 character long string so we trim it down to 5 
$security_code = substr($md5_hash, 16, 5);

//Set the session to store the security code
$_SESSION["security_code"] = $security_code;

Now set width and height and create an image and fill it with white color using ImageFill function as below.

//Set the image width and height
$width = 205;
$height = 60; 

//Create the image resource 
$image = ImageCreate($width, $height);  

//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);

//Make the background white 
ImageFill($image, 0, 0, $white);

Now to write the security code on image with black color using imagestring function. Also to increase some complexity you can draw lines using imageline function as below. For the text I have used ‘hootie.gdf‘ font, you can change this whatever you like.

$font = imageloadfont('hootie.gdf');
imagestring($image, $font, 50, 20, $security_code, $black);

//Throw in some lines to make it a little bit harder for any bots to break 
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/2, $width, $height/2, $grey); 
imageline($image, $width/2, 0, $width/2, $height, $grey); 
 
//Tell the browser what kind of file is come in 
header("Content-Type: image/jpeg"); 

//Output the newly created image in jpeg format 
ImageJpeg($image);
   
//Free up resources
ImageDestroy($image);

You can see this image captcha on browser by putting the url/captcha.php. Now you can use this captcha url as image source and ask the visitor to enter code, what you see in image captcha.

On form submission you can check this code with your stored session value. If you want it case-insensitive you can use strtolower for both values and than check as below.

if(strtolower($_SESSION['security_code']) == strtolower($_POST['code'])){
    echo "valid code";
} else {
    echo "invalid code";
}

So, This is the simple captcha implementation example we have so far. You can take some guidance from this and make your own code to generate image captcha. This script requires only PHP GD library and little setup to implement.

LIVE DEMO

Generate and Implement Simple Captcha Code In PHP
Generate and Implement Simple Captcha Code In PHP