How to make CAPTCHA using PHP

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to make CAPTCHA using PHP

Post by Tony » Sun Nov 29, 2009 4:09 am

A CAPTCHA (an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart", trademarked by Carnegie Mellon University) is a type of challenge-response test used in computing to determine whether or not the user is human.

This CAPTCHA script presents a distorted image with six letters. It is relatively easy for a human to see the letters and enter them into a response box on your input form. Then you send the response back to the script for verification.

In your html form, put an <img> tag for the image and an input box, as in the example below. Do not put width or height parameters in the img tag as this will distort the generated image.

Code: Select all

<img src="captcha.php" onclick="this.src='captcha.php?'+Math.random();return false;" alt="captcha image missing" title="" /> 
<br /><span style='font-size: 14px;'>click the image to get new letters</span> 
<br />Please enter the letters in the box below<br /> 
<input type='text' name='captcha' style='width: 60px;' autocomplete='off' />  
Then in your processing script you include the CAPTCHA script as in the example below:

Code: Select all

$validate = $_POST['captcha']; 
include('captcha.php'); 
if ($validate != "valid") $alert = "Wrong verification code entered";  
Name the script below captcha.php.

Code: Select all

<?php
//
// to validate the user entered value the variable $validate must be populated with the user input
// $validate will return with either "valid" or "invalid"
// if $validate is not set then a captcha image is generated
//
// to make the captcha harder for a bot to read, the font is random for each character and there are lines in the image
session_start();
if (isset($validate)) {
  // this section validates the user input
  if (strlen($validate) == 6) {
    $validate = strtoupper($validate);  // make input upper case for compare
    if ($_SESSION['captcha'] == $validate) $validate = "valid"; else $validate = "invalid";
  } else $validate = "invalid";
  unset($_SESSION['captcha']);
} else {
  // this section generates the captcha image
  // make the character array 
  $chars = array();
  for ($i = 65; $i <= 90; $i++) $chars[] = chr($i);
  // generate values
  for ($i = 1; $i <= 6; $i++) { $key = rand(0,count($chars)-1); $text[$i] = $chars[$key]; }
  $_SESSION['captcha'] = $text[1].$text[2].$text[3].$text[4].$text[5].$text[6];
  // create image
  $im = @imagecreate(70,25);
  $backgroundColor = imagecolorallocate($im,150,150,150);
  $textColor = imagecolorallocate($im,0,0,0);
  $lineColor = imagecolorallocate($im,100,100,100);
  imageline($im,0,0,23,25,$lineColor);
  imageline($im,23,0,46,25,$lineColor);
  imageline($im,46,0,69,25,$lineColor);
  imageline($im,0,25,70,0,$lineColor);
  imagestring($im,rand(3,5),5,5,$text[1],$textColor);
  imagestring($im,rand(3,5),15,5,$text[2],$textColor);
  imagestring($im,rand(3,5),25,5,$text[3],$textColor);
  imagestring($im,rand(3,5),36,5,$text[4],$textColor);
  imagestring($im,rand(3,5),45,5,$text[5],$textColor);
  imagestring($im,rand(3,5),55,5,$text[6],$textColor);
  imagepng($im);
  imagedestroy($im);
}
?>
Post Reply

Return to “PHP & MySQL”