Monday, 2 July 2012

Tagged under: , , ,

How to create CAPTCHA using PHP

CAPTCHA:  Completely Automated Public Turing Test To Tell Computers and Humans Apart.

A CAPTCHA is a program that protects websites against bots by generating and grading tests that humans can pass but current computer programs cannot. For example, humans can read distorted text as the one shown alongside, but current computer programs can't:

The term CAPTCHA (for Completely Automated Public Turing Test To Tell Computers and Humans Apart) was coined in 2000 by Luis von Ahn, Manuel Blum, Nicholas Hopper and John Langford of Carnegie Mellon University.

Generating a simple CAPTCHA and its verification is quiet a simple task using PHP. In this post, I would do the same, but the CAPTCHA generated would be a simple one, while the reader can add his own creativity to it later!!

  • Step I: Create a file captchaimg.php, and add the following code to it:

header("Content-Type: image/png");
$text=substr(md5(uniqid(rand(), true)),0,5);
$_SESSION["vercode"] = $text; 
$height = 25; 
$width = 65; 
$image_p = imagecreate($width, $height); 
$black = imagecolorallocate($image_p, 0, 0, 0); 
$white = imagecolorallocate($image_p, 255, 255, 255); 
$font_size = 14; 
imagestring($image_p, $font_size, 5, 5, $text, $white); 
imagejpeg($image_p, null, 80); 

You can check the above file by opening it in your browser. Everytime you refresh, a new, random alphanumeric string is generated in the CAPTCHA.
  • Step II: Create a form form.php, and add the following code to it:
if ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["vercode"]=='')
     echo  '<strong>Incorrect verification code.</strong>'; 
  } else { 
     // add form data processing code here 
     echo  '<strong>Verification successful.</strong>'; 
<form action="form.php" method="post"> 
Comment: <input type="text" name="coment"> 
Enter Code <img src="captchaimg.php" /><input name="vercode" type="text" /> 
<input name="Submit" type="submit" value="Submit" /> 

Now, this is it!! Your basic CAPTCHA is ready!! It will look like below:

Another Example:
For this, you need to include a font file in your project folder. I have used AngelicWar.ttf. Download the font from the download box alongside.
Now, Overwrite the file captchaimg with the following code:

header("Content-type: image/png");
$im = imagecreate(105, 50); //Size of the image Width, Height

imagecolorallocate($im, 167, 218, 239);  //Set background color 
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);        

$font = 'AngelicWar.ttf'; // You can replace font by your own        
for($i=0;$i<=5;$i++) {
    $numb=substr(md5(uniqid(rand(), true)),0,1);
    $angle=rand(-25, 25);
    imagettftext($im, 20, $angle, 8+15*$i, 30, $black, $font, $numb);    
    // Add shadow to the text    
    imagettftext($im, 20, $angle, 9+15*$i, 34, $grey, $font, $numb);    

Now, this will result in following CAPTCHA:

For the following CAPTCHA use cheapink.ttf from the download box alongside.

You can enhance it by using two different strings in a single CAPTCHA image, or using some string characteristics.

Want help in creating more creative CAPTCHA?? Feel free to Contact Me.


  1. Very helpful post!! Thanks!!Continue the good work:-)

  2. Nice post..Can you tell me how to do this in Class?

  3. Can you elaborate on your requirements??

  4. You have done creating CAPTCHA in simple php.But can you tell me how to do the same in class like php's oop concept?

  5. so good information and the codes so is very useful

    image decoding

  6. This Captcha Information is very nice and this article is very useful to learners and easy to reach for all others... the captcha using php Coding and instructions is great method and easy to handling... thanks for the captcha information,