Prevent Spambot with PHP CAPTCHA

Completely Automated Public Turing test to tell Computers and Humans Apart is known as CAPTCHA is used to determine whether the system is used by human or not. Or in simple word CAPTCHA is used to prevent sending SPAM data in a HTML form. Usually  there are 3 types of captcha used in HTML and PHP. Image captcha, Mathematical captcha and voice/ Audio captcha. You can also use reCAPTCHA provided by Google, it is free, more secure and easy to integrate.

Image CAPTCHA

Image captcha display text or number in an image and ask the user to enter the same image content in the input box. If the user enter the exact word then it will process the form, otherwise it will display error message. Image captcha generated using PHP GD library and Session variable.

Generate Image CAPTCHA

The following php code will generate an image with a number between 10000 to 99999. Save the file as captcha.php.
<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;

$height = 22; //CAPTCHA image height
$width = 60; //CAPTCHA image width

$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 12; 
imagestring($image_p, $font_size, 5, 3, $text, $white);
imagejpeg($image_p, null, 80);
?>

Call CAPTCHA image in your HTML form

<html>
<head>
<title>Image Captcha</title>
</head>
<body>
<form action="#null" method="post">
Enter Code: <input name="captcha" type="text">
<img src="captcha.php" /><br>
<input name="submit" type="submit" value="Submit">
</form>
</body>
</html>

PHP Code to Validate CAPTCHA

<?php
session_start();
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&
  $_SESSION["vercode"]==$_POST["captcha"]){
  //Continue
}
else{
  $msg = "Wrong verification code.";
  header("Location:yourfile.php?msg=$msg");
}
?>
 

Mathematical CAPTCHA

Mathematical captcha ask a very simple addition or subtraction to the user, if the user answered it correct then it will processed. Mathematical captcha are very easy to generate with PHP rand() function and Session variables. But you can't run a Mathematical Captcha in a HTML page, your page has to be PHP page.
  

Generate Mathematical Captcha

Save the following code as mathcaptcha.php and include the file in your form page.
<?php
session_start();

$n1=rand(1,6); //Generate First number between 1 and 6 
$n2=rand(5,9); //Generate Second number between 5 and 9 
$answer=$n1+$n2; 

$math = "What is ".$n1." + ".$n2." : "; 
$_SESSION['vercode'] = $answer;

print $math;
?>

Include captcha file in your page

<html>
<head>
<title>Image Captcha</title>
</head>
<body>
<form action="savedata.php" method="post">
<?php include('mathcaptcha.php');?> <input name="captcha" type="text">
<br>
<input name="submit" type="submit" value="Submit">
</form>
</body>
</html>

Validate your Captcha

<?php
session_start();
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&
$_SESSION["vercode"]==$_POST["captcha"]){
//Continue
}
else{
$msg = "Wrong Answer.";
header("Location:yourfile.php?msg=$msg");
}
?>

Sample Registration form with Captcha.

Download Files Total Downloads: 1620
Comments
comments powered by Disqus