PHP - Disable Login In 3 Attempts

In this tutorial we will create a Disable Login In 3 Attempts using PHP. This code will automatically disable a login button in a 3 consecutive invalid login attempts. The code use SESSION to store a number of attempts everytime the user login an invalid account. This is a user-friendly kind of program feel free to modify it. We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_attempt, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_attempt");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <?php session_start()?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Disable Login In 3 Attempts</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3">
  19. <h5>Username: admin</h5>
  20. <h5>Password: admin</h5>
  21. </div>
  22. <div class="col-md-6">
  23. <form method="POST" action="">
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" name="password" class="form-control" required="required" />
  31. </div>
  32.  
  33. <?php include 'login.php'?>
  34.  
  35. <center><button class="btn btn-primary" name="login" <?php if(ISSET($_SESSION['msg'])){ echo $_SESSION['msg'];}?>><span class="glyphicon glyphicon-log-in"></span> Login</button> <a class="btn btn-success" href="reset.php"><span class="glyphicon glyphicon-refresh"></span> Reset</a></center>
  36. </form>
  37. </div>
  38. </div>
  39. </body>
  40. </html>

Creating Reset Function

This code contains the reset function of the application. This code will reset the entire sessions when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as reset.php.
  1. <?php
  2.  
  3. header("location: index.php");
  4. ?>

Creating the Main Function

This code contains the main function of the application. This code will disable the login button in a 3 invalid logins. To make this just copy and write these block of codes below inside the text editor, then save it as login.php
  1. <?php
  2. if(ISSET($_POST['login'])){
  3. require 'conn.php';
  4.  
  5. $username=$_POST['username'];
  6. $password=$_POST['password'];
  7.  
  8. $query=mysqli_query($conn, "SELECT * FROM `user` WHERE `username`='$username' AND `password`='$password'") or die(mysqli_error());
  9. $row=mysqli_num_rows($query);
  10.  
  11. if($row > 0){
  12. echo "<center><label class='text-success'>Login success!</label></center>";
  13. }else{
  14. if(!ISSET($_SESSION['attempt'])){
  15. $_SESSION['attempt'] = 0;
  16. }
  17.  
  18. $_SESSION['attempt'] += 1;
  19.  
  20. if($_SESSION['attempt'] === 3){
  21. $_SESSION['msg'] = "disabled";
  22. }
  23.  
  24.  
  25. echo "<center><label class='text-danger'>Invalid username or password</label></center>";
  26. }
  27. }
  28. ?>
There you have it we successfully created Disable Login In 3 Attempts using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment