QAds
Apple iPad 32gb Wi-Fi
$548.97
Apple iPod nano 8 GB Siyah
$88.21
Coby 2.8-Inch LCD Touchscreen Video MP3 Player
$52.49
Toshiba Satellite T115D-S1125 LED TruBrite Laptop
$360.00
:: HOME PHP Human Check - Security Code
 
Human Check - Security Code

Probably you see the human check code that a kind of image shows you some letters and want you to enter it into input field to realize whether you are a human or not. Why people use it that to prevent flood attact which are automatic codes that sends thousands of submit data to your WEBsite form and gets down it.

So lets see how to do that. At first you need GD open at your WEB host. If your codes doesnt work it means There are 2 reason, one is you mistype some thing or there is no GD support at your WEB host. contact you host tech and request GD support.

At first we need to create database tables to keep human check code.

Click Here To Download SecurityCode.SQL File

Then we need a PHP file to show our human check code to visitors.

 

<?PHP
if (isset($HTTP_GET_VARS["refid"]) && $HTTP_GET_VARS["refid"]!="") {
   $referenceid = stripslashes($HTTP_GET_VARS["refid"]);
} else {
   $referenceid = md5(mktime()*rand());
}

$font = "courbd.ttf";

$bgurl = rand(1, 3);
$im = ImageCreateFromPNG("bg".$bgurl.".png");

$chars = array("a","A", "b","B","c","C","d","D","e","E", "f","F","g","G","h","H", "i","I","j","J","k",
"K","l", "L","m","M","n","N","p","P","q","Q","r","R","s", "S","t","T","u","U","v",
"V","w", "W","x","X","y","Y","z","Z","1","2","3", "4","5","6","7","8","9");
if(isset($_GET['t']))
    $length = $_GET['t'];
else
    $length = 5;
$textstr = "";
for ($i=0; $i<$length; $i++) {
   $textstr .= $chars[rand(0, count($chars)-1)];
}

$size = rand(14, 16);
$angle = rand(-5, 5);
$color = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100));

$textsize = imagettfbbox($size, $angle, $font, $textstr);
$twidth = abs($textsize[2]-$textsize[0]);
$theight = abs($textsize[5]-$textsize[3]);
$x = (imagesx($im)/2)-($twidth/2)+(rand(-20, 20));
$y = (imagesy($im))-($theight/2);

ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $textstr);

header("Content-Type: image/png");
ImagePNG($im);

imagedestroy($im);

MySQL_connect("localhost", "DBUSER", "DBPASSWORD") or die(MySQL_error());
MySQL_select_db("DBNAME");

MySQL_query("INSERT INTO security_images (insertdate, referenceid, hiddentext) VALUES (now(), '".$referenceid."', '".$textstr."')");

MySQL_query("DELETE FROM security_images WHERE insertdate < date_sub(now(), interval 1 day)");

exit;

?>

 

Here i want to exlain some places. we will send our security code(human check code) with refid variable. Also with t variable we can determine how many chars we want to show. Here I used $db variable for my database transaction. To learn about it please check my PHP/Database_with_PHP.HTML">database with PHP document. As you see you need font file and png files to use this script.

Click here To Download Font and PNG Files

Then you can use it where ever you want like below index.php file

<?php
    $refid = md5(mktime()*rand());
    $check = TRUE;
    if(isset($_POST['submit']))
    {
        $secCheck = $db->query("SELECT ID FROM security_images WHERE referenceid = ".$db->check($_POST['security_refid'])." AND hiddentext= ".$db->check($_POST['security_try']));
        if(!$db->sql_num_rows()>0) $check = FALSE;
       
        if($check == TRUE) {
            // do what ever you want here
        }
    }
$refid = md5(mktime()*rand());
?>
<?php if($check == FALSE) echo "Please Check Human Check Code!"; ?>
<form action="index.php" method="post">
ID : <input type="text" name="id"><br>
Password : <input type="password" name="password"><br>
Human Check Code : <img src="securityimage.php?refid=<?php echo $refid; ?>" align="absmiddle"><br>
<input type="hidden" name="security_refid" value="<?php echo $refid; ?>">
Human Check Code : <input name="security_try" type="text" id="security_try" size="10" maxlength="10"><br>
<input type="submit" name="submit" value="submit">
</form>

Above I create $refid but you dont have to do that, if you dont do that securityimage.php file will create it automatically. After I post data, I check it like above code and then I do what ever I want. In this example I do simple login page.