Click here to Skip to main content
6,822,123 members and growing! (18,482 online)
Email Password   helpLost your password?
Web Development » HTML / CSS » General     Beginner License: The Code Project Open License (CPOL)

Securing Web Forms with Simple PHP-CAPTCHA

By sujithfem

CAPTCHA an acronym for “completely automated public Turing test to tell computers and humans apart." CAPTCHA technology enables you to discern human requests from computer generated requests on the Web, where such a distinction is difficult. Simply defined, "Man can read; machine can’t!”
Win2K, PHP, Dev
Posted:20 Jul 2006
Views:28,960
Bookmarked:8 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 2.15 Rating: 2.77 out of 5
2 votes, 33.3%
1

2
1 vote, 16.7%
3
2 votes, 33.3%
4
1 vote, 16.7%
5

Introduction

In web available forms are always prone to attack by people who want to use your application for their own purposes. Many web sites use the CAPTCHA especially used to prevent bots from using various types of computing services. The applications include preventing bots from taking part in online polls, registering for free email accounts, more recently, preventing bot-generated spam by requiring that the (unrecognized) sender pass a CAPTCHA test before the email message is delivered [implemented in Yahoo]. They have also been used to prevent people from using bots to assist with massive downloading of content from multimedia websites.

You have probably seen the CAPTCHA project in action at some of your Web destinations. Its principal tool is a randomly created image that contains a phrase unmentioned in computer-readable text on the rendered page. The form asks the user to provide the phrase. If the form post does not contain the correct phrase, you can safely assume either the human made a user error, or it wasn't a human at all.

Using the Code

Now it's time to put this code to work. A simple and often-used interface to implement this new security measure is the form on website. In this form you typically capture random numbers.

<form name="form1" method="post" action="form.php" ">
    <table width="342" align="center" cellspacing="0" bgcolor="#D4D0C8">
        <tr> 
            <td align="center">
                <img src="php_captcha.php">
            </td>
            <td align="center"> 
                Please enter the string shown in the image in the form.<br>
            </td>
            <td align="center">
                <input name="number" type="text">
            </td>
            <td>
                <input name="Submit" type="submit" value="Submit">
            </td> 
        </tr>
    </table>
</form>

The following code use to create random numbers and this number are embedding with existing image file, the first line used to initiate session, which use to carry the user inputs.

<?php

session_start();
$RandomStr = md5(microtime());
$ResultStr = substr($RandomStr,0,5);
$NewImage =imagecreatefromjpeg("img.jpg");

?>

The second line [md5 (microtime ())] use to generate the random string, and the resultant string is trim by using third line [substr], which returns the portion of string specified by the start and length parameters. The function imagecreatefromjpeg ("img.jpg") is use to create a image by existing image file and as back ground ,so that you need to give an image file path.

<?php

$LineColor = imagecolorallocate($NewImage,233,239,239);
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);
imageline($NewImage,1,1,40,40,$LineColor);
imageline($NewImage,1,100,60,0,$LineColor);
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor); 

?>

After creation of back ground image, we generate some linear line, which is use to avoid the phrasing from random numbers, the respective lines are create by the function named imageline () and imagestring () use to draw a random string horizontally.

<?php 

$_SESSION['key'] = $ResultStr;
?>

The resultant random number [trimmed one], carry through session especially for validation purpose.

<?php

header("Content-type: image/jpeg");
imagejpeg($NewImage);

?>

Finally above two functions are uses to display/out put the image to browser. So we can just call the particular file by through image source path, it will display the final image.

<?php

if(isset($_REQUEST['Submit'])){
    $key=substr($_SESSION['key'],0,5);
    $number = $_REQUEST['number'];
    if($number!=$key){
        echo ' Validation string not valid! Please try again!';
    }
    else
    {
        echo ' Your string is valid!';
    } 
}

?>

I hope you know about the above code functionality, it’s about validating the user in put and actual random number, depends upon the application you may use the if and else conditions, that’s all

Conclusion

CAPTCHA can be a great way to limit the amount of successful, unwanted HTTP POST requests in your application, CAPTCHAs are by definition fully automated, requiring little human maintenance or intervention in administering the test. This has obvious benefits in cost and reliability; I hope the simple code is useful to understand the concept. Happy CAPTCHA-ing!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

sujithfem


Member
Sujith,I Began tinkering with web development as professional in 2004 ,currently working in the IT department ,Insoft,I has experience developing web solution with java script ,PHP,TAL,MYSQL,Oracle and Apache and animation solution with Flash ,Swish and sound Forge.
If you want to know about me more just click my family portal, it’s www.triqtra.co.nr


Occupation: Web Developer
Location: India India

Other popular HTML / CSS articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
Generalthanks, good and simple description PinmemberDanielf197022:35 16 Jul '09  
GeneralVisual Flash CAPTCHA PinmemberSearcherx10:18 9 Feb '07  
Generalsure (from a .net point of view) Pinmemberpointnetsolutions8:53 21 Jul '06  
GeneralOkay but.. PinmemberLaubi0:58 21 Jul '06  
GeneralRe: Okay but.. Pinmembersujithfem21:57 24 Jul '06  
GeneralI have also written some php Captcha [modified] PinmemberPriyank Bolia23:01 20 Jul '06  
GeneralRe: I have also written some php Captcha Pinmembersujithfem21:55 24 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 20 Jul 2006
Editor: Genevieve Sovereign
Copyright 2006 by sujithfem
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project