A Simple CAPTCHA Script Using PHP

CAPTCHA stands for “Completely Automated Public Turing test to tell Computers and Humans Apart”, it’s very common functionality to use at the time of submit data to prevent machines access of your website. In this post we will developed CAPTCHA script suing PHP with help of easy three steps.This is very basic captcha which is generated by PHP.

Following files will use in this Captcha Example

  • catpcha.jpg – A Image for catcha
  • captcha.php – This file is use to generate captcha with PHP using PHP session.
  • index.php – This file is use to show captcha image to end user.

You can also check other tutorial for CAPTCHA,

Demo Sample

Captcha-with-php

There are following steps need to add CAPTCHA Using PHP

Step 1-We will put the capcha image in project folder.

This image will used to generate captcha image using PHP GD library.This is a only dependency(GD library) with this example.

Download Image

You can use save as image option as well using right click.

Step 2- We need to create a new file captcha.php file in project and put the blow PHP code into this file.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php  
session_start();  
header("Expires: Mon, 26 Jul 1997 05: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");   
  
function _generateRandom($length=6)  
{  
    $_rand_src = array(  
        array(48,57) //digits  
        , array(97,122) //lowercase chars  
//      , array(65,90) //uppercase chars  
    );  
    srand ((double) microtime() * 1000000);  
    $random_string = "";  
    for($i=0;$i<$length;$i++){  
        $i1=rand(0,sizeof($_rand_src)-1);  
        $random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));  
    }  
    return $random_string;  
}  
  
$im = @imagecreatefromjpeg("captcha.jpg");   
$rand = _generateRandom(3);  
$_SESSION['captcha'] = $rand;  
ImageString($im, 5, 2, 2, $rand[0]." ".$rand[1]." ".$rand[2]." ", ImageColorAllocate ($im, 0, 0, 0));  
$rand = _generateRandom(3);  
ImageString($im, 5, 2, 2, " ".$rand[0]." ".$rand[1]." ".$rand[2], ImageColorAllocate ($im, 255, 0, 0));  
Header ('Content-type: image/jpeg');  
imagejpeg($im,NULL,100);  
ImageDestroy($im);  
?>

This file is used to generate CAPTCHA image using GD library and rendered to the view page of project where it will call.

Step 3-We will create template file index.php in project folder and put below PHP code into this file.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<div class="container">
<h1>A Simple Example Of PHP CAPTCHA Script</h1>
<?php  
if(isset($_POST["captcha"]))  
if($_SESSION["captcha"]==$_POST["captcha"])  
{  
    //CAPTHCA is valid; proceed the message: save to database, send by e-mail …  
    echo '<div class="alert alert-success">CAPTHCA is valid; proceed the message</div>';  
}  
else  
{  
    echo '<div class="alert alert-danger">CAPTHCA is not valid; ignore submission</div>';  
}  
?>
<form role="form" method="post">
  <div class="form-group">
    <label for="email">Email address:</label>
    <input type="email" class="form-control" id="email">
  </div>
  <div class="form-group">
    <label for="pwd">Password:</label>
    <input type="password" class="form-control" id="pwd">
  </div>
   <div class="form-group">
    <div class="col-sm-5 pull-left"><label for="pwd">Anti Spam code, Please Enter 3 Black Symbols</label>
    <img src="captcha.php" alt="captcha image"></div>
    <div class="col-sm-7 pull-right"><input type="text" name="captcha" size="3″ maxlength="3″ class="form-control"></div>
  </div>
  <div class="form-group" style="padding-top:75px;">
    <button type="submit" class="btn btn-primary">Submit</button>
  </div>
</form>
</div>

Here i am creating HTML form to show capcha image and display captcha validation message to user.

Demo & Download Code Of CAPTCHA Script with PHP

Please feel free to send queries to me using below comment section.