PHP - Simple Login with md5

Submitted by: 
Language: 
Visitors have accessed this post 397 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Login with md5</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3">
  19. <h5>DEFAULT USER</h4>
  20. <h6>Username: admin</h6>
  21. <h6>Password: admin</h6>
  22. </div>
  23. <div class="col-md-6">
  24. <form method="POST" >
  25. <div class="form-group">
  26. <label>Username</label>
  27. <input type="text" name="username" maxlength="50" class="form-control" required="required"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Password</label>
  31. <input type="password" name="password" maxlength="12" class="form-control" required="required"/>
  32. </div>
  33. <?php include 'login.php'?>
  34. <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  35. </form>
  36. </div>
  37. </div>
  38. </body>
  39. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  18. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  19. </a> </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.  
  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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.