Jump to content

Recommended Posts

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
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]<?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(97, 122);
break;
case 2:
$IMGVER_RandVal = mt_rand(48, 57);
break;
case 3:
$IMGVER_RandVal = mt_rand(65, 90);
break;

}

return chr($IMGVER_RandVal);

}

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

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

[code]<?php
$IMGVER_IMAGE = imagecreate(110,40);

$IMGVER_COLOR_BLACK = imagecolorallocate($IMGVER_IMAGE, 0, 0, 0);
$IMGVER_COLOR_WHITE = imagecolorallocate($IMGVER_IMAGE, 255, 255, 255);

imagefill($IMGVER_IMAGE, 0, 0, $IMGVER_COLOR_BLACK);

session_start();
$IMGVER_RandomText = $_SESSION["IMGVER_RndText"];

imagechar($IMGVER_IMAGE, 4, 20, 13, $IMGVER_RandomText[0] ,$IMGVER_COLOR_WHITE);
imagechar($IMGVER_IMAGE, 5, 40, 13, $IMGVER_RandomText[1] ,$IMGVER_COLOR_WHITE);
imagechar($IMGVER_IMAGE, 3, 60, 13, $IMGVER_RandomText[2] ,$IMGVER_COLOR_WHITE);
imagechar($IMGVER_IMAGE, 4, 80, 13, $IMGVER_RandomText[3] ,$IMGVER_COLOR_WHITE);

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

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]<?php
if(isset($_POST['submitButton'])) {
if($_POST['captchaTextField'] == $_SESSION['IMGVER_RndText']) {
// Handle your form here...
}
}
?>[/code]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.