OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting

Search This Site :     PHP Function Reference :
 

Image Verification using PHP

By admin (2006-10-31. 8596 views.)
This tutorial shows how to create an image on the fly that can be used for form validation. Nice way to prove you have a human hitting the submit button.

In php all graphics are done using the gd library. So, first you need to make sure that you have the gd2 extension enabled on your machine.

There are two small scripts in this tutorial:(1) image.php - generates the image and displays the form and (2) verify.php - verifies the string typed in matches.

Script 1 - image.php

<?php

/*tell browser that an image is coming */
Header("Content-Type: image/png");

/* initialize a session - used to store the verification string. */
session_start();

/* grab the session id - use it for the image name to avoid collisions for multiple users */
$sessionid session_id();

/* create an image big enough to hold 4 characters */
$im ImageCreate(4040); 

/* use white for text, black for image background */
$white ImageColorAllocate($im255255255);
$black ImageColorAllocate($im000);

/*seed the random string generator.*/
srand((double)microtime()*1000000); 

/*muck it up*/
$string md5(rand(0,9999)); 

/*get 4 characters from the random string (32 characters long) */
$new_string substr($string124);

/*save in session so we can test to verify on the form receipt */
$_SESSION['new_string'] = $new_string;

 
/*paint the background of the image */
ImageFill($im00$black);

 
/*writes string  into image $im using font, x, y, string, color */
ImageString($im4410$new_string$white);

/* send the image to the browser (mime component) */
ImagePNG($im"$sessionid.png");
ImageDestroy($im); 
?>
<html>
<head>
<title>Image Verify</title>
</head>
<body>
    Type the code you see in the image in the box below. (case insensitive)

    <img src="<? echo $sessionid;?>.png">
    <form action="verify.php" method="post">
        <input name="verify" type="text" value="">

        <input type="submit">
    </form>
</body>
</html>

?>


Some notes on image.php:
- Many sites use interesting effects on the string presented. I guess the idea is that some machine could do character recognition on the same font over and over. You can vary the font using gd routines.
- Many sites use varying colors and/or patterns applied to the image. Again, I am assumed to avoid recognition routines. You would have to develop a series of patterns and pick a random one to apply in the particular instance.
- Almost any random generator will do. You may be able to use a sufficiently large enough set of prefabricated images/'random' strings so you can just pull up image x instead of the bothering with generating one from scratch. Although it doesn't appear to be an expensive process.
- I am using the session id for the image name to avoid collisions on a higher traffic site.
- Be sure to mark the image for deletion. I don't think this is automatic.

Script 2 - verify.php

<?php

/*This starts the php session stuff*/
session_start(); 

/*grab the value entered*/
$random trim($_POST['verify']);

/*grab the random string we stored*/
$new_string $_SESSION['new_string'];

/*make sure they are the same*/
if (strtolower($new_string) == strtolower($random)){
    echo 
"Success";
}
else{
    echo 
"Try again.";
}

?>


Notes on verify.php:
- I think this is fairly clean session use.
- You may want to force the image.php page to expire so that people/programs can't just go back and try again.

Note: the code is using the php session to store a random string. You may want to disable php session id being displayed in the url.
 

 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting


Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS Free Templates

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005