Captcha Generation in PHP

, , Leave a comment

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.

$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);
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);
$_SESSION['key'] = $ResultStr;
header("Content-type: image/jpeg");

Part Two: Image Captcha Integration in HTML

Save the below file as index.php.

<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 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">
            <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="">Back to Article</a></h2>

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

  $number = $_REQUEST['number'];  
        echo ' Wrong Code Entered! Please try again!';
        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.