Easy to use php captcha

Here I am posting code of an easy to use and customizable php captcha code.You can change the font and number of characters easily by changing to lines of code.If you want to change the color you can also do that.Here is the code.

php code to generate the captcha image
<?php
session_start();
$length=6;/*Change according to the length of string you want*/
$dir="font/cowboys.ttf";/*directory of ttf font file in which you wish to display the captcha code*/
$a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$string="";
$len=$length;
while($len!=0)
{
$string=$string.$a[rand(0,strlen($a)-1)];
$len--;
}
$_SESSION['captcha']=$string;
$image=imagecreatetruecolor(($length*30),60);
$black=imagecolorallocate($image,0,0,0);
$white=imagecolorallocate($image,255,255,255);
imagefilledrectangle($image,0,0,($length*40),100,$white);
imagettftext($image,30,0,10,40,$black,$dir,$_SESSION['captcha']);
header("Content-type: image/png");
imagepng($image);


?>


save it as captcha.php

The html form to display the captcha

<html>
<head>
<TITLE></TITLE>
<form action="check.php" method="post" enctype="multipart/form-data">
<img src="captcha.php"/><br/><br></br>
<input type='text' name='capt'/>
<input type='submit'/>
</head>
<body>
</body>
</html>



File that validate the users captcha entry

session_start();
if(isset($_POST['capt']) && $_POST['capt']==$_SESSION['captcha'])
{
echo "CAPTCHA ENTERED IS CORRECT";
}
else
{
echo "Incorrect captcha";
}
?>


save it as check.php

I will post the explanation within 2 days enjoy


No comments:

Post a Comment