Show Captcha on Login Failed Attempts

Show Captcha on Login Failed Attempts

If you are looking for Show Captcha on Login Failed Attempts then you are at the right place. In the previous tutorial, we already saw about user login and PHP captcha. In this tutorial, we are going to combine this two tutorial to create a powerful login using captcha. If the user tried more than 3 failed login attempts, then the captcha will show to add control for invalid login. In the example below, we have to calculate the number of failed login attempts based on the IP Address. More than 3 failed login attempts, then the captcha will be displayed to the user to continue the login. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding.

Markup of Login Form and Captcha

This simple HTML source code contains login form and captcha code. Where the user can type the account details to log in.
  1. <form method="post" class="form_style" action="">
  2.         <div class="message"><?php if($message_alert!="") { echo $message_alert; } ?></div>
  3.         <table cellpadding="10" cellspacing="" width="500" align="center">
  4.                 <tr class="tableheader">
  5.                         <td align="center" colspan="2" style="font-size:20px; color:blue; font-family:cursive;">Enter Login Details</td>
  6.                 </tr>
  7.                 <tr class="tablerow">
  8.                         <td align="right">Username</td>
  9.                         <td><input class="input_style" type="text" name="user_name" autofocus="autofocus"></td>
  10.                 </tr>
  11.                 <tr class="tablerow">
  12.                         <td align="right">Password</td>
  13.                         <td><input class="input_style" type="password" name="password"></td>
  14.                 </tr>
  15.                 <?php if (isset($failed_attempt_to_login) && $failed_attempt_to_login >= 3) { ?>
  16.                 <tr class="tablerow">
  17.                         <td align="right"></td>
  18.                         <td><input name="captcha_code" type="text"><br><br><img src="captcha_code.php" /></td>
  19.                 </tr>
  20.                 <?php } ?>
  21.                 <tr class="tableheader">
  22.                         <td align="center" colspan="2"><input type="submit" class="button_style" name="submit" value="Submit"></td>
  23.                 </tr>
  24.         </table>
  25. </form>

Captcha Source Code

This simple source code to create a captcha code.
  1. <?php
  2.  
  3. $random_alpha = md5(rand());
  4. $captcha_code = substr($random_alpha, 0, 6);
  5. $_SESSION["captcha_code"] = $captcha_code;
  6. $target_layer = imagecreatetruecolor(70,30);
  7. $captcha_background = imagecolorallocate($target_layer, 200, 110, 119);
  8. imagefill($target_layer,0,0,$captcha_background);
  9. $captcha_text_color = imagecolorallocate($target_layer, 0, 0, 0);
  10. imagestring($target_layer, 5, 5, 5, $captcha_code, $captcha_text_color);
  11. header("Content-type: image/jpeg");
  12. imagejpeg($target_layer);
  13.  
  14. ?>

Output

Simple Log In Form Result If the user tries more than 3 attempts to log in. This would be the result. Result Try in the Live Demo below. Note: You can customize your Login Form whatever your desired design by changing the CSS. For the full source code, kindly click the "Download Code" button below. Hope that this simple tutorial that I created may help you to your future projects. If you are interested in programming, we have an example of programs that may help you even just in small ways. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment