Create dynamic captcha image using simple php functions to validate form data. This is a very basic code and beginners also understand easily.

Captcha for form validation, This is some extensive code I will remove in next revision of this blog.

<?php
session_start();

// generate random number and store in session
$number = rand(99, 9999);
$number= str_replace(0, 1, $number);
// Character+Number+character
$randomnr = substr(‘drtaPaRfadFriandDFfGgThWjDkjNkPllyIp’,rand(0,20), 1).$number.substr(‘drtaPaRfadFrandiDFfGgThWjDkjNkPllyIp’,rand(0,20), 1);
$_SESSION['randomnr2'] = md5($randomnr);

//generate image
$im = imagecreatetruecolor(120, 55);

//colors:
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

$bluee = imagecolorallocate($im, 215, 225, 235);
$blue2 = imagecolorallocate($im, 113, 148, 196);

imagefilledrectangle($im, 1, 1, 118, 53, $bluee);
imagefilledrectangle($im, 1, 10, 118, 45, $blue2);

//path to font: Please upload a font file and specify path here
$font = ‘text.ttf’;

/* draw text */
// Shadow text, If you need then place otherwise comment.
imagettftext($im, 20, 18, 20, 40, $grey, $font, $randomnr);
// VALIDATION CAPTCHA CODE TEXT
imagettftext($im, 20, 15, 20, 40, $black, $font, $randomnr);

// prevent client side  caching
header(“Expires: Wed, 1 Jan 2010 00: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”);

//send image to browser
header (“Content-type: image/gif”);
imagegif($im);
imagedestroy($im);
?>

You can add this captcha using single line of code:

1.] save this code in a file “captcha.php”

2) Place this img tag where you want to put captcha for form validation <img src=”captcha.php” alt=”” title=”Security code” />

Output captcha is look like this:

 

[NOTE: Please upload any font file and place, then set path in script for variable $font.  (e.g. $font = 'text.ttf';  relative PATH from captcha.php) ]

Comment :