Wednesday, 4 January 2012

PHP Human Verification ( captcha ) Script.

Step 1 Generate a String of ramdom cheractors
function random_string()
{
$string="";
$c="";//Random char buffer
$i=0;
for($i=0;$i<=4;$i++)
{
$s = rand(65,90);//generates a random cheractor and store in $s
$string=$string.sprintf("%c",$s);//Can-cads random character to $string.
} 
return $string;
} 



Step 2 Store the generated String in a secession variable.
session_start();
$_SESSION["key"]=random_string();


Step 3 Display generated string as an image in html forum.
function for image generation.
function generate_textimage($string)
{
        $myImage = imagecreate(200, 50);
        $myGray = imagecolorallocate($myImage, 250, 255, 255);
        $black = imagecolorallocate($myImage, 0, 0, 0);
        putenv('GDFONTPATH=' . realpath('.'));

        $font='pirulen.ttf';
        imagettftext($myImage, 25, 0, 20, 30, $black, $font, $string);
imagefilter($myImage, IMG_FILTER_GAUSSIAN_BLUR);
        ob_start();
        imagepng($myImage);
        printf('<img src="data:image/png;base64,%s"/>', 
                base64_encode(ob_get_clean()));
        imagedestroy($myImage);
}

Over all usage

<pre class="brush:[php];"><html>
    <head>

        <title></title>
    </head>
    <body>
<?php
require_once("huveri.php");
session_start();
//Generate Random String
$string = random_string();

$_SESSION["key"]=$string;

?>

        <?php generate_textimage($string); ?>

        <form action="vali.php" method="post">
        <input type="text" name="key" />
        <input type="submit" value="Submit" />
        </form>
    </body>
</html>




Step 4 Do some verification in form action.


<?php
session_start();

if($_SESSION["key"]==$_POST["key"])
{
    echo "Wellcome Human";
   
    }else {
        echo "Your not Human";
        }

session_destroy();

?>

Saturday, 31 December 2011

Date select field in html forum using php.



Download php file.

<select name=<?php echo"'$date'";?> >
<option value ='0' >Date</option>
<?php
$date=1;
while($date<=31)
{
echo "<option value = '$date'>$date</option>";
++$date;
}
?>
</select>
<select  name=<?php echo"'$month'";?> >
<option value ='0' >Month</option>
<?php
$month = array(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December");
$i=0;
while($i<=11)
{
echo "<option value = '$month[$i]'>$month[$i]</option>";
++$i;
}
?>
</select>
<select name=<?php echo"'$year'";?> >
<option value ='0' >Year</option>
<?php
$year=2012;
while($year>=1900)
{
echo "<option value = '$year'>$year</option>";
--$year;
}
?>
</select>.



Usage
<?php
$year="dobyar";
$month="dobmonth";
$date="dobdate";
require("date.php");
?>



Exmaple


<html>
<head>
<title>Form</title>
</head>
<body>
<form>
<table summary="" >
<tr><td>
<?php $year="dobyear"; 
$date="dobdate"; 
$month="dobmonth"; 
require("date.php");  
?><
/td></tr>
<tr><td><input type="submit" value="Submit" /></td></tr>
</table>
</form>
</body>
</html>