How to include Captcha in php program / script

captcha

sample captcha code

//captcha.php

<?php
session_start();
if(isset($_REQUEST[‘submit’]))
{
if(isset($_REQUEST[“captcha”])&&$_REQUEST[“captcha”]!=””&&$_SESSION[“code”]==$_REQUEST[“captcha”])
{
echo “Correct Code Entered”;
//Do you stuff
}
else
{
echo “wrong code try again”;
}
}
?>
<html>
<body>
<form action=”captcha.php” method=”post”>
Enter Image Text
<input name=”captcha” type=”text”>
<img src=”captcha_img.php” /><br>
<a href=”captcha.php”>Refresh</a>
<input name=”submit” type=”submit” value=”Submit”>
</form>
</body>
</html>

Here is a php code to generate image with random number

//captcha_img.php

<?php
session_start();
$code=rand(1000,9999);
$_SESSION[“code”]=$code;
$im = imagecreatetruecolor(50, 24);
$bg = imagecolorallocate($im, 192, 126, 220);
$fg = imagecolorallocate($im, 255, 255, 255);
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);
?>

output

cap

Leave a Comment