Php

A Simple CAPTCHA Script Using PHP

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

The following files will use in this Captcha Example

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

Demo Sample

Implement CAPTCHA Using PHP

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

This image will be used to generate a captcha image using the 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.

Related Post

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

<?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 a CAPTCHA image using the GD library and rendered to the view page of the project where it will call.

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

<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>

In the above code, I am creating an HTML form to show a captcha image and display captcha validation messages to the user.

Demo & Download Code Of CAPTCHA Script with PHP

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

Tags: php captcha

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories