Author Topic: Enter a random code to make sure I am human  (Read 578 times)

0 Members and 1 Guest are viewing this topic.

Offline kenwvsTopic starter

  • Enthusiast
  • Posts: 195
    • View Profile
Enter a random code to make sure I am human
« on: July 16, 2006, 04:01:30 AM »
Sometimes when you complete a form you have to enter a random code of numbers and letters to make sure that you are human.... what is this called and is it done in PHP or some other language?

I have an online order form that we want to look at implementing this and am wondering if this is a complicated process?

thanks,

Ken

Offline Gast

  • Devotee
  • Posts: 601
  • Gender: Male
  • AJAX... not the cleaner type however...
    • View Profile
    • Niall McMahon
Re: Enter a random code to make sure I am human
« Reply #1 on: July 16, 2006, 05:09:17 AM »
Not really. It is called a captcha and can be used as long as you have the GD library extension installed with PHP (most installations do by default).

The code for this is below. In a file called "captcha.php" which you include where you want to display the actual image put this code:

Code: [Select]
<?php
$IMGVER_TempString 
"";
for(
$i=1$i<=4$i++) {
$IMGVER_TempString .= GetRandomChar();
}

$_SESSION["IMGVER_RndText"] = $IMGVER_TempString;

function 
GetRandomChar() {

mt_srand((double)microtime()*1000000);

$IMGVER_RandVal mt_rand(1,3);

switch ($IMGVER_RandVal) {

case 1:
$IMGVER_RandVal mt_rand(97122); 
break;
case 2:
$IMGVER_RandVal mt_rand(4857);
break;
case 3:
$IMGVER_RandVal mt_rand(6590);
break;

}

return chr($IMGVER_RandVal);

}

echo 
'<img src="img.php?'.SID.'" border="0" />';
?>

Then you will need another file called "img.php" which generates the random text on the image:

Code: [Select]
<?php
$IMGVER_IMAGE 
imagecreate(110,40);

$IMGVER_COLOR_BLACK imagecolorallocate($IMGVER_IMAGE000);
$IMGVER_COLOR_WHITE imagecolorallocate($IMGVER_IMAGE255255255);

imagefill($IMGVER_IMAGE00$IMGVER_COLOR_BLACK);

session_start();
$IMGVER_RandomText $_SESSION["IMGVER_RndText"];
 
imagechar($IMGVER_IMAGE42013$IMGVER_RandomText[0] ,$IMGVER_COLOR_WHITE);
imagechar($IMGVER_IMAGE54013$IMGVER_RandomText[1] ,$IMGVER_COLOR_WHITE);
imagechar($IMGVER_IMAGE36013$IMGVER_RandomText[2] ,$IMGVER_COLOR_WHITE);
imagechar($IMGVER_IMAGE48013$IMGVER_RandomText[3] ,$IMGVER_COLOR_WHITE);

header("Content-type: image/jpeg");
imagejpeg($IMGVER_IMAGE);
?>

Then on your form when you test if the form has been submitted also check the captcha matches a text field that you have placed next to it (in this case called "captchaTextField"):

Code: [Select]
<?php
if(isset($_POST['submitButton'])) {
if($_POST['captchaTextField'] == $_SESSION['IMGVER_RndText']) {
// Handle your form here...
}
}
?>

« Last Edit: July 16, 2006, 05:10:56 AM by Gast »
OS and Internet Specs:
Windows XP (with SP2), 1680x1050, PHP 4, MySQL 3, Internet Explorer 6 &7 /FireFox 2.0/Opera 9.2

Tutorials:
Simple Introduction to AJAX and XMLHttpRequest
Creating Live Data with AJAX Making a Google Suggest like application