Creating a Login App With md5 Encryption Using PHP

In this tutorial we will create a Login App With md5 Encryption using PHP. This code will protect the user login information by encrypting the password with md5. The code use a php POST method to call a special php built-in function md5() an advance encrypting tool that protect the user password by just passing the string in order to encrypt it into a protected password.

We will be using PHP as a scripting language and interpreter that is used primarily on any web server including xampp, wamp, etc. It is being used on famous websites and it has a modern technology that can easily be used 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_login_md5, after that click Import then locate the database file inside the folder of the application then click ok.

tut1

You can also add the sample user table programmatically. Just copy and paste the script below in the SQL field in PHPMyAdmin and click GO.

  1. CREATE TABLE `user` (
  2.   `username` varchar(50) NOT NULL,
  3.   `password` text NOT NULL,
  4.   `name` varchar(100) NOT NULL
  5.  
  6. INSERT INTO `user` (`user_id`, `username`, `password`, `name`) VALUES
  7. (1, 'admin', '21232f297a57a5a743894a0e4a801fc3', 'Admnistrator');

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_login_md5");
  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">Login App With md5 Encryption Using PHP Source Code</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-4">
  18.                         <form method="POST"     >      
  19.                                 <div class="form-group">
  20.                                         <label>Username</label>
  21.                                         <input type="text" name="username" maxlength="50" class="form-control" required="required"/>
  22.                                 </div>
  23.                                 <div class="form-group">
  24.                                         <label>Password</label>
  25.                                         <input type="password" name="password" maxlength="12" class="form-control" required="required"/>
  26.                                 </div>
  27.                                 <?php include 'login.php'?>
  28.                                 <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  29.                         </form>
  30.                 </div>
  31.                 <div class="col-md-3">
  32.                         <h5>DEFAULT USER</h4>
  33.                         <h6>Username: admin</h6>
  34.                         <h6>Password: admin</h6>
  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">Login App With md5 Encryption Using PHP Source Code</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-4">
  31.                         <h1>WELCOME</h1>
  32.                         <h3 class="text-primary"><?php echo $fetch['name']?></h3>
  33.                         <a href="logout.php">Logout</a>
  34.                 </div>
  35.         </div>
  36. </body>
  37. </html>

Creating the Main Function

This code contains the minor function of the application. This code will login the user account with a md5 encryption. 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. ?>

DEMO

There you have it, we successfully created a Login App With md5 Encryption using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

Comments

that is the best

Add new comment