MD5 Login using PDO in PHP

This work is suited for the beginners who want to know coding in PHP using PDO. This source code will help you on how to create MD5 Login using PDO in PHP. The feature of this simple tutorial, it has an input field validations does not allow empty field in the web page and the target of this work is the password are being encrypted using the function of MD5 using PHP in PDO query. Hope that this source code will help you in your future work or projects. Thank you. We are going to create simple markup for the Login form field. Study the source code below. This source code below where the user types their information such as the Email Address and their Password to enter to the next page of the web page. The password is encrypted, so much better to memorize your password or you have to create a password that you can easily know.
  1. <div>
  2. <form method="post" action="login-query.php">
  3. <div>
  4. <label>Email Address</label>
  5. <input type="text" name="email" placeholder="Email Address . . . . ." autofocus="autofocus" required>
  6. </div>
  7. <div>
  8. <label>Password</label>
  9. <input type="password" name="password" placeholder="Password . . . . .">
  10. </div>
  11. <button type="submit" name="submit">Login</button>
  12. </form>
  13. </div>
And, this is the query for login using PDO in PHP. This query executes if the Email Address and the Password of the user is accurate to the record in the database. All we need to log in is the email address and the password of the user from the database. This query constructed in PHP using PDO. After enters the information to log in, the user will directly to the new page of the web page. If the information is not accurate to the record in the database. The system has a trapper which the user can receive an alert message that the information is incorrect. This source code, get the information from the form field where the user enters the information such as email address and password. Then, it will check to the database if the information given by the user was correct in the record of the database.
  1. <?php
  2. include ('database.php');
  3.  
  4. $email = $_POST['email'];
  5. $password = md5($_POST['password']);
  6.  
  7. $query = $database->query("Select * from tbl_user where email = '$email' and password ='$password' ");
  8. $count = $query->rowcount();
  9. $row = $query->fetch();
  10. ?>
This query executes if the user information was correct to the record of the database. It will direct to the next page of the web page.
  1. <?php
  2. if ($count > 0)
  3. {
  4. $_SESSION['id'] = $row['tbl_user_id'];
  5. header('location:home.php');
  6. }
  7. ?>
But, if the user information was incorrect, this source code helps us to trap the information to disallow the user to enter in the next page of the web.
  1. <script>
  2. alert("The combination of User Name and Password is invalid!")
  3. window.location = "index.php";
  4. </script>
For the full source code for the login query.
  1. <?php
  2. include ('database.php');
  3.  
  4. $email = $_POST['email'];
  5. $password = md5($_POST['password']);
  6. $query = $database->query("Select * from tbl_user where email = '$email' and password ='$password' ");
  7. $count = $query->rowcount();
  8. $row = $query->fetch();
  9.  
  10. if ($count > 0)
  11. {
  12. $_SESSION['id'] = $row['tbl_user_id'];
  13. header('location:home.php');
  14. }
  15. else
  16. {
  17. ?>
  18. <script>
  19. alert("The combination of User Name and Password is invalid!")
  20. window.location="index.php";
  21. </script>
  22. <?php
  23. }
  24. ?>
If you have a question regarding this tutorial, you can leave a comment below. For the full source code, kindly download the code below. Enjoy coding. Thank you.

Add new comment