Sep 26, 2013

Ketan Patel

Captcha Generation in PHP

Captcha Generation in PHP

What Does Captcha Stand For?

A Captcha is a type of challenge-response test used in website as an attempt to ensure that the response is generated by a human    being.A Captcha is one kind of  program that can generate and  also test that humans can pass but computer programs cannot. Say for example, humans can read distorted text, but current computer programs can't:


 CAPTCHA means "Completely Automated Public Turing test to tell Computers and Humans Apart".    

Types of Captcha  

There are basically 3 types of captcha.They are as below.
(1)    Image Captcha
(2)    Mathematical Captcha
(3)    Voice Captcha.

Uses of Captcha

•    To Prevent  Comment Spam in Blogs
•    Website Registration Protection
•    Online Polls
•    To  Prevent Dictionary Attacks
•    Search Engine Bots
•    Email Worms and Spam.

Among all the types as given above,image captcha is most poular and widely used.So,In this tutorial we will learn how to generate and use image captcha using php.
This tutorial is divided into mainly three parts.We will see them one by one.

Part One: Image Captcha Generation   

Here is php code to generate image with random code. Save it as captcha.php.First start session to store captcha code in session then generate random code using php rand() function and write it on generated image.

<?php
session_start();
$ResultStr = '';
for ($i = 0; $i < 5; $i++) {
$ResultStr .= chr(rand(97, 122));
}

$NewImage =imagecreatefromjpeg("img.jpg");
$LineColor = imagecolorallocate($NewImage,233,239,239);
$TextColor = imagecolorallocate($NewImage, 127, 127, 127);
imageline($NewImage,1,1,40,40,$LineColor);
imageline($NewImage,1,100,60,0,$LineColor);
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);
$_SESSION['key'] = $ResultStr;
header("Content-type: image/jpeg");
imagejpeg($NewImage);
?>


Part Two: Image Captcha Integration in HTML

Save the below file as index.php.

<?php
session_start();
?>
<html>
<form name="post" method="post" action="post.php">
<div class="captcha_example">
<div class="top"><p>Captcha generation in PHP | PHP Dev Zone</p></div>
    <div class="inner">
        <div>
            <div style="margin-bottom:10px;">
                    <h4>Captcha image:</h4>
                    <img src="captcha.php" class="form_captcha" />
                    <div class="lines">Verification (Type what you see):</div>
                    <input name="number" type="text" class="captcha">
                    <input name="Submit" type="submit" class="main-color-bg" value="Submit">
            </div>
            <p>Here  captcha image is being generated using GD library, each time when page loads it display different combinations of symbols.</p>
            <h2 style="float: right;"><a href="http://www.php-dev-zone.com/2013/09/captcha-generation-in-php.html">Back to Article</a></h2>
        </div>
    </div>
</div>
</form>
</html>

Part Three: Image Captcha Response Validation

First start session to access $_SESSION[“key”] variable then compare it with posted captcha response. If both are same then run your code else exit from code with message Wrong Code Entered.Save below file as post.php

<?php
session_start();
if(isset($_REQUEST['Submit'])){
  $number = $_REQUEST['number'];  
  $key=$_SESSION['key'];
  
    if($number!=$key){
        echo ' Wrong Code Entered! Please try again!';
    }
    else
    {
        echo ' Your string is valid!';
    //You can make query to insert the data into database here.
    }
}
?>

That’s it. Simple Captcha Generation in PHP is Completed.


Ketan Patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :