Menu

Sunday, September 14, 2014

Capcha Code In php






This blog explain how to create a captcha  in PHP. by  using some of the features of PHP for creating an image.  This blog explain how to create and compare the captcha code with user entered code and also put captcha validation. below we have explain captcha example in php (below we have explain captcha example in php.).




Advantages.
  • we can Use One or More PNG Backgrounds Image.
  • we can Use One or More TTF or OTF fonts
  • we can Set Minimum or Maximum Code Lengths.
  • we can Configure Which Characters To Use
  • we can Set Minimum or Maximum Font Size.
  • Adjustable Angle, Font Color, and Shadow of Font.

--------------------
capcha.php
<?php
session_start();
$code=rand(1000,9999);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(50, 24);
$bg = imagecolorallocate($im, 22, 86, 165); //background color blue
$fg = imagecolorallocate($im, 255, 255, 255);//text color white
imagefill($im, 0, 0, $bg);
imagestring($im, 5, 5, 5,  $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
--------------------------------
index.php
<?php session_start(); ?>
<html>
<head>
<title>Test Form</title>
</head>
<body>
<form method="post">

Enter Image Text
<input name="captcha" type="text">
<img src="capcha.php" /><br>
<input name="submit" type="submit" value="Submit">
</form>
</body>
</html>


<?php

if(isset($_POST["captcha"]))
{
    if($_POST["captcha"]==$_SESSION["code"])
    {
    echo "Correct";
    }
    else
    {
    echo "Not Correct";
    }
}
?>

No comments:

Post a Comment