PHP Captcha Demo - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Captcha Demo - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, June 15, 2017

PHP Captcha Demo


PHP Captcha Demo

PHP captcha




Since we Know Captcha is very Important for our site to prevent our site from misuse or spamming then we will use captcha then attackers or bots not spam in our site.Lets start Creating PHP Captcha for our site.

First : form.php
Code :

<html>
<head>
<title>Check It</title></head>
<body>
<form action="val.php" method="post">
<img src="captcha.php">
</br>
Enter Image Text
<input type="text" name="cap">
</br>
<input type="submit" name="s" value="Submit">
</form>
</body>
</html>

This is simple html form where we print our captcha and input box for captcha value input then we validate its true or false.

Second : captcha.php
Code :

<?php
session_start(); //Starting Session
$code=rand(100000,999999); //Random Number
$_SESSION['code']=$code; //Storing Captcha vallue in session
$im=imagecreatetruecolor(100,40);
$bg=imagecolorallocate($im,80,60,165);
$fg=imagecolorallocate($im,50,255,255);
imagefill($im,5,5,$bg);
imagestring($im,20,30,10,$code,$fg);

//Creating Image in Png fomat

header('content-type:image/png');
imagepng($im);
imagedestroy($im);
//Destroying Image
?>

This is Captcha generating page it generates captcha in png image format and store captcha value in session then we check the session captcha value and user captcha value are same or not.

Third : val.php
Code:

<?php
session_start();
$a=$_POST['cap']; //receiving cap value from form
$b=$_SESSION['code']; //Getting Captcha Value from Session

//Comparing User Captcha value and Session Value
if(isset($a) && $a !="" && $b==$a)
{
echo "correct code enter"; //True Captcha
}
else
{
die ("wrong code entered"); //False Captcha
}
?>

In The Last Page val.php we check captcha is true or false.

Thank You

No comments:

Post a Comment

Post Top Ad