Today I am going to explain you how we create captcha in PHP. It required at many places for user verification so that our system can't be used by any robot or by any automated system, It can be used at the time of login into the system or at the time of registration and so on. Now the question arise that how we generate captcha image in PHP don't worry i am going to explain you using a simple example.
Step1: Create a file index.php
<html>
<title>Captcha Verification in PHP</title>
<body style="background-color: antiquewhite">
<form action="verify.php" method="post">
Enter Verification Code <br/><br/><img src="captcha.php">
<br/><br/>
<input type="text" name="code" value=""/>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<title>Captcha Verification in PHP</title>
<body style="background-color: antiquewhite">
<form action="verify.php" method="post">
Enter Verification Code <br/><br/><img src="captcha.php">
<br/><br/>
<input type="text" name="code" value=""/>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
Step2: Create a file captcha.php
<?php
session_start();
// To randomly display number between 100000 to 999999
$verification_code = rand(100000,999999);
// Store the randomly generated $verification_code to session variable verify
$_SESSION["verify"] = $verification_code;
// Set the height and width of background display behind the verification code
$height = 30;
$width = 60;
// Now we have to create image and set the background color and font color
$buildimage = imagecreate($width, $height);
$backcolor = imagecolorallocate($buildimage, 0, 255, 165);
$fontcolor = imagecolorallocate($buildimage, 0, 175, 255);
$font_size = 16;
imagestring($buildimage, $font_size, 5, 5, $verification_code, $fontcolor);
// in below line 40 means quality of font it varies from 0 to 100 (0 means worse and 100 means best quality)
imagejpeg($buildimage, null, 40);
?>
session_start();
// To randomly display number between 100000 to 999999
$verification_code = rand(100000,999999);
// Store the randomly generated $verification_code to session variable verify
$_SESSION["verify"] = $verification_code;
// Set the height and width of background display behind the verification code
$height = 30;
$width = 60;
// Now we have to create image and set the background color and font color
$buildimage = imagecreate($width, $height);
$backcolor = imagecolorallocate($buildimage, 0, 255, 165);
$fontcolor = imagecolorallocate($buildimage, 0, 175, 255);
$font_size = 16;
imagestring($buildimage, $font_size, 5, 5, $verification_code, $fontcolor);
// in below line 40 means quality of font it varies from 0 to 100 (0 means worse and 100 means best quality)
imagejpeg($buildimage, null, 40);
?>
Step3: Create a file verify.php
<?php
session_start();
// Compare the code enter by user and code generated by system to verify the user
if ($_POST["code"] != $_SESSION["verify"] OR $_SESSION["verify"]==' ') {
echo '<strong>Incorrect verification code.</strong>';
} else {
echo '<strong>Verification successful.</strong>';
}
?>
session_start();
// Compare the code enter by user and code generated by system to verify the user
if ($_POST["code"] != $_SESSION["verify"] OR $_SESSION["verify"]==' ') {
echo '<strong>Incorrect verification code.</strong>';
} else {
echo '<strong>Verification successful.</strong>';
}
?>
Output
Note: If you like this post or have any question in your mind then just drop me a comment I will reply you as soon as possible.
9 Comments
First of all thank you for sharing this informative blog.. this blog really helpful for everyone.. explanation are clear so easy to understand... I got more useful information from this blog
ReplyDeletephp training institute in chennai velachery | best php training institute in chennai velachery
thanku for sharing this information.this code is very useful for me visit us web developer in Jaipur
ReplyDeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging… Java Training Center in Chennai | Best J2EE Training Center in Chennai | No.1 Java Training Institution in Velachery
ReplyDeleteNice and good article.. it is very useful for me to learn and understand easily..
ReplyDeleteBest Mobile Computing Project Center in Chennai | Mobile Computing Project Center in Velachery
Wonderful post to updating..Thank you.DotNet Project Center in Chennai | DotNet Project Center in Velachery
ReplyDelete
ReplyDeleteThanks for sharing the information, Salesforce experts a lot of openings in multi-level companies, for more information n
Salesforce Training
Professional Salesforce CRM TrainingSalesforce Training online in India
pretty post..! This content is very helpful for developing my knowledge and I want additional information about this topic. Please posting and sharing with us...
ReplyDeleteSpark Training in Chennai
Spark Training
Pega Training in Chennai
Primavera Training in Chennai
Embedded System Course Chennai
Linux Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai
Corporate Training in Chennai
Unix Training in Chennai
Thanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!
ReplyDeleteVMware Training in Chennai
Vmware Training center in Chennai
VMware Training institute in chennai
Machine Learning Training in Chennai
Blue Prism Training in Chennai
UiPath Training in Chennai
Automation Anywhere Training in Chennai
VMwareTraining in Vadapalani
VMwareTraining in Guindy
VMwareTraining in Thiruvanmiyur
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. 토토인증업체
ReplyDelete