Skip to main content

PHP Md5 Password Convert to Original


Getting Started

The method is simple as creating new table wherein it will insert both the original password and the md5 hash equivalent and retrieve it using your query.

Please take note that bootstrap used in this tutorial is hosted so you need internet connection for them to work.

Creating our Database

1. Open phpMyAdmin.
2. Click databases, create a database and name it as test.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

User table:

  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR(50) NOT NULL,
  4. `password` VARCHAR(50) NOT NULL,
  5. PRIMARY KEY(`userid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Mds Hash Table:

  1. CREATE TABLE `md5password` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `original` VARCHAR(50) NOT NULL,
  4. `md5hash` VARCHAR(50) NOT NULL,
  5. PRIMARY KEY(`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

how to sql

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.

  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "test");
  4.  
  5. if ($conn->connect_error) {
  6. die("Connection failed: " . $conn->connect_error);
  7. }
  8.  
  9. ?>

index.php

This is our index which contains our Sign up Form. I've created a simple register so that we can enter data to our users table and simple login.

  1. <!DOCTYPE html>
  2. <title>PHP Md5 Password Convert to Original</title>
  3. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  5. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  6. </head>
  7. <div class="container">
  8. <h1 class="page-header text-center">PHP Md5 Password Convert to Original</h1>
  9. <div class="col-md-4 col-md-offset-4">
  10. <div class="login-panel panel panel-primary">
  11. <div class="panel-heading">
  12. <h3 class="panel-title"><span class="glyphicon glyphicon-user"></span> Register
  13. <a href="login.php" class="pull-right">Login</a>
  14. </h3>
  15. </div>
  16. <div class="panel-body">
  17. <form method="POST" action="register.php">
  18. <div class="form-group">
  19. <input class="form-control" placeholder="Username" name="username" id="username" type="text" required autofocus>
  20. </div>
  21. <div class="form-group">
  22. <input class="form-control" placeholder="Password" name="password" id="password" type="password" required>
  23. </div>
  24. <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-pencil"></span> Sign up</button>
  25. </form>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </body>
  31. </html>

register.php

This is our simple PHP code to register.

  1. <?php
  2. include('conn.php');
  3. $username=$_POST['username'];
  4. $original=$_POST['password'];
  5. $password=md5($_POST['password']);
  6.  
  7. $sql="insert into user (username,password) values ('$username','$password')";
  8. $conn->query($sql);
  9.  
  10. $sql="insert into md5password (original, md5hash) values ('$original', '$password')";
  11. $conn->query($sql);
  12.  
  13. header('location:login.php');
  14. ?>

login.php

This is our simple login.

  1. <?php
  2. if(isset($_SESSION['userid'])){
  3. header('location:home.php');
  4. }
  5. ?>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <title>PHP Md5 Password Convert to Original</title>
  10. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  11. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  12. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <h1 class="page-header text-center">PHP Md5 Password Convert to Original</h1>
  17. <div class="col-md-4 col-md-offset-4">
  18. <div class="login-panel panel panel-primary">
  19. <div class="panel-heading">
  20. <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Login
  21. <a href="index.php" class="pull-right">Register</a>
  22. </h3>
  23. </div>
  24. <div class="panel-body">
  25. <form method="POST" action="verify.php">
  26. <fieldset>
  27. <div class="form-group">
  28. <input class="form-control" placeholder="Username" name="username" id="username" type="text" required autofocus>
  29. </div>
  30. <div class="form-group">
  31. <input class="form-control" placeholder="Password" name="password" id="password" type="password" required>
  32. </div>
  33. <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  34. </fieldset>
  35. </form>
  36. </div>
  37. </div>
  38.  
  39. <?php
  40. if(isset($_SESSION['error'])){
  41. ?>
  42. <div class="alert alert-danger text-center">
  43. <?php echo $_SESSION['error']; ?>
  44. </div>
  45. <?php
  46.  
  47. unset($_SESSION['error']);
  48. }
  49. ?>
  50. </div>
  51.  
  52. </div>
  53. </body>
  54. </html>

verify.php

This is to validate the login. Note: This is just a simple login validation. If you want, I have several tutorials on validations. Feel free to visit them.

  1. <?php
  2. include('conn.php');
  3.  
  4. $username=$_POST['username'];
  5. $password=md5($_POST['password']);
  6.  
  7. $sql="select * from user where username='$username' and password='$password'";
  8. $query=$conn->query($sql);
  9.  
  10. if($query->num_rows > 0){
  11. $row=$query->fetch_array();
  12. $_SESSION['userid']=$row['userid'];
  13. header('location:home.php');
  14. }
  15.  
  16. else{
  17. $_SESSION['error']="Login Failed. Invalid Login";
  18. header('location:login.php');
  19. }
  20.  
  21. ?>

home.php

This is our goto page after a successful login and this will also show to details of the user that logged in with the original password.

  1. <?php
  2. include('conn.php');
  3.  
  4. $sql="select * from user where userid='".$_SESSION['userid']."'";
  5. $query=$conn->query($sql);
  6. $row=$query->fetch_array();
  7.  
  8. $psql="select * from md5password where md5hash='".$row['password']."'";
  9. $pquery=$conn->query($psql);
  10. $prow=$pquery->fetch_array();
  11. ?>
  12.  
  13. <!DOCTYPE html>
  14. <html>
  15. <head>
  16. <title>PHP Md5 Password Convert to Original</title>
  17. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  18. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  19. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="jumbotron text-center">
  24. <div class="container">
  25. <h1>Welcome to MySite</h1>
  26. <p class="lead">This is a our Goto Page after a successful Login. You login details will shown below.<p>
  27. </div>
  28. </div>
  29. <div class="row">
  30. <h2>Your details:</h2>
  31. <ul style="list-style-type:none;">
  32. <li>Username: <?php echo $row['username']; ?></li>
  33. <li>Password: <?php echo $prow['original']; ?></li>
  34. </ul>
  35. </div>
  36. <div class="row">
  37. <a href="logout.php" class="btn btn-primary"><span class="glyphicon glyphicon-log-out"></span> Logout</a>
  38. </div>
  39. </div>
  40. </body>
  41. </html>

logout.php

Lastly, this is our logout code.

  1. <?php
  2. header('location:login.php');
  3. ?>

That ends this tutorial. Happy 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.