Skip to main content

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. require_once 'conn.php';
  4.  
  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="https://sourcecodester.com">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">PHP - Simple Login with md5</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. ?>
  28.  
  29. <div class="col-md-3"></div>
  30. <div class="col-md-6">
  31. <h1>WELCOME</h1>
  32. <h3 class="text-primary"><?php echo $fetch['name']?></h3>
  33. <a href="logout.php" class="btn btn-primary"><span class="glyphicon glyphicon-log-out"></span> 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 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.  
  3. require_once 'conn.php';
  4.  
  5. if(ISSET($_POST['login'])){
  6. $username = $_POST['username'];
  7. $password = md5($_POST['password']);
  8.  
  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);
  12.  
  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. ?>

logout.php

  1. <?php
  2. unset($_SESSION['user_id']);
  3. header("location: index.php");
  4. ?>

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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.