PHP - Simple Login with md5

In this tutorial we will create a Simple Login with md5 using PHP. This code can protect the user login information by encrypting the password value. The code use a special php built-in function md5() a very strong encrypting tool that protect the user password by just passing the string in order to ultimately encrypt into a powerful password. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.

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_md5, 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_password");
  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 shown below. index.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple Login with md5</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-3">
  18.                         <h5>DEFAULT USER</h4>
  19.                         <h6>Username: admin</h6>
  20.                         <h6>Password: admin</h6>
  21.                 </div>
  22.                 <div class="col-md-6">
  23.                         <form method="POST"     >      
  24.                                 <div class="form-group">
  25.                                         <label>Username</label>
  26.                                         <input type="text" name="username" maxlength="50" class="form-control" required="required"/>
  27.                                 </div>
  28.                                 <div class="form-group">
  29.                                         <label>Password</label>
  30.                                         <input type="password" name="password" maxlength="12" class="form-control" required="required"/>
  31.                                 </div>
  32.                                 <?php include 'login.php'?>
  33.                                 <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  34.                         </form>
  35.                 </div>
  36.         </div>
  37. </body>
  38. </html>
home.php
  1. <!DOCTYPE html>
  2. <?php
  3.         session_start();
  4.         require_once 'conn.php';
  5.        
  6.         if(!ISSET($_SESSION['user_id'])){
  7.                 header('location: index.php');
  8.         }
  9. ?>
  10. <html lang="en">
  11.         <head>
  12.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  13.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  14.         </head>
  15. <body>
  16.         <nav class="navbar navbar-default">
  17.                 <div class="container-fluid">
  18.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  19.                 </div>
  20.         </nav>
  21.         <div class="col-md-3"></div>
  22.         <div class="col-md-6 well">
  23.                 <h3 class="text-primary">PHP - Simple Login with md5</h3>
  24.                 <hr style="border-top:1px dotted #ccc;"/>
  25.                 <?php
  26.                         $query=mysqli_query($conn, "SELECT * FROM `user` WHERE `user_id`='$_SESSION[user_id]'") or die(mysqli_error());
  27.                         $fetch=mysqli_fetch_array($query);
  28.                 ?>
  29.                
  30.                 <div class="col-md-3"></div>
  31.                 <div class="col-md-6">
  32.                         <h1>WELCOME</h1>
  33.                         <h3 class="text-primary"><?php echo $fetch['name']?></h3>
  34.                         <a href="logout.php" class="btn btn-primary"><span class="glyphicon glyphicon-log-out"></span> Logout</a>
  35.                 </div>
  36.         </div>
  37. </body>
  38. </html>

Creating the Main Function

This code contains the minor function of the application. This code will allow the user to login with a md5 encrypted protection. To do this just kindly write these block of codes inside the text editor, then save it as shown below. login.php
  1. <?php
  2.         session_start();
  3.        
  4.         require_once 'conn.php';
  5.        
  6.         if(ISSET($_POST['login'])){
  7.                 $username = $_POST['username'];
  8.                 $password = md5($_POST['password']);
  9.                
  10.                 $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());
  11.                 $rows = mysqli_num_rows($query);
  12.                 $fetch = mysqli_fetch_array($query);
  13.                
  14.                 if($rows > 0){
  15.                         $_SESSION['user_id'] = $fetch['user_id'];
  16.                         header("location: home.php");
  17.                 }else{
  18.                         echo "<center><label class='text-danger'>Invalid username or password!</label></center>";
  19.                 }
  20.         }
  21. ?>
logout.php
  1. <?php
  2.         session_start();
  3.         unset($_SESSION['user_id']);
  4.         header("location: index.php");
  5. ?>
There you have it we successfully created a Simple Login with md5 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