Tuesday, May 21, 2013
Simple Captcha in PHP with javascript Validation
<html>
<head>
<title>Captcha validation </title>
<script>
function validateForm()
{
var val = document.forms ["myForm"]["captcha"].value;
if(val == null || val =="")
{
alert ("Please enter the Captcha on Text Box");
return false ;
}
}
</script>
</head>
<body>
<form name="myForm" action="validate.php" method="POST" onSubmit="return validateForm()">
Enter Image Text
<input name="captcha" type="text">
<img src="captcha.php" /><br>
<input name="submit" type="submit" value="Captcha Validation">
</form>
</body>
</html>
captcha.php
<?php
session_start();
$code=rand(1000,9999);
$_SESSION["code"]=$code;
$img = imagecreatetruecolor(50, 24);
$bgnd = imagecolorallocate($img, 22, 86, 165);
$fgnd = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $bgnd);
imagestring($img, 5, 5, 5, $code, $fgnd);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
validate.php
<?php
session_start();
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
echo "Correct Code Entered";
//Do you stuff
}
else
{
die("Wrong Code Entered");
}
?>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment