Generating Captcha in PHP for Verification


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>


 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);
?>


 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>';
}
?>


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. 



Post a Comment

9 Comments

  1. 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

    php training institute in chennai velachery | best php training institute in chennai velachery

    ReplyDelete
  2. thanku for sharing this information.this code is very useful for me visit us web developer in Jaipur

    ReplyDelete
  3. Good 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

    ReplyDelete

  4. Thanks 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

    ReplyDelete
  5. 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