Skip to main content

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

And, this is the link for the bootstrap that i used for the layout design

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.


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
  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");
  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.

  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="">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>
  1. <!DOCTYPE html>
  2. <?php
  3. require_once 'conn.php';
  5. if(!ISSET($_SESSION['user_id'])){
  6. header('location: index.php');
  7. }
  8. ?>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  12. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  13. </head>
  14. <body>
  15. <nav class="navbar navbar-default">
  16. <div class="container-fluid">
  17. <a class="navbar-brand" href="">Sourcecodester</a>
  18. </div>
  19. </nav>
  20. <div class="col-md-3"></div>
  21. <div class="col-md-6 well">
  22. <h3 class="text-primary">Login App With md5 Encryption Using PHP Source Code</h3>
  23. <hr style="border-top:1px dotted #ccc;"/>
  24. <?php
  25. $query=mysqli_query($conn, "SELECT * FROM `user` WHERE `user_id`='$_SESSION[user_id]'") or die(mysqli_error());
  26. $fetch=mysqli_fetch_array($query);
  27. ?>
  29. <div class="col-md-4">
  30. <h1>WELCOME</h1>
  31. <h3 class="text-primary"><?php echo $fetch['name']?></h3>
  32. <a href="logout.php">Logout</a>
  33. </div>
  34. </div>
  35. </body>
  36. </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.

  1. <?php
  3. require_once 'conn.php';
  5. if(ISSET($_POST['login'])){
  6. $username = $_POST['username'];
  7. $password = md5($_POST['password']);
  9. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());
  10. $rows = mysqli_num_rows($query);
  11. $fetch = mysqli_fetch_array($query);
  13. if($rows > 0){
  14. $_SESSION['user_id'] = $fetch['user_id'];
  15. header("location: home.php");
  16. }else{
  17. echo "<center><label class='text-danger'>Invalid username or password!</label></center>";
  18. }
  19. }
  20. ?>
  1. <?php
  2. unset($_SESSION['user_id']);
  3. header("location: index.php");
  4. ?>


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!

Add new comment