Skip to main content

PHP - Easy Login Using PDO

In this tutorial we will create a Easy Login Using PDO using PDO. This code can login an user account with PDO query when the user click the login button. The code use a PDO SELECT query to validate the data inputs whether it is existed in the database server. This is a user friendly kind of program, feel free to modify it.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

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_pdo_login, after that click Import then locate the database file inside the folder of the application then click ok.

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 = new PDO('mysql:host=localhost;dbname=db_pdo_login', 'root', '');
  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 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="">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 - Easy Login Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2">
  18. <h5>Default login</h5>
  19. <h6>Username: admin</h6>
  20. <h6>Password: admin</h6>
  21. </div>
  22. <div class="col-md-8">
  23. <div class="panel panel-primary">
  24. <div class="panel-heading">
  25. <h4>Login</h4>
  26. </div>
  27. <div class="panel-body">
  28. <form method="post" action="">
  29. <div class="form-group">
  30. <label>Username</label>
  31. <input type="text" name="username" class="form-control" required="required"/>
  32. </div>
  33. <div class="form-group">
  34. <label>Password</label>
  35. <input type="password" name="password" class="form-control" required="required"/>
  36. </div>
  37. <?php include'login.php'?>
  38. <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  39. </form>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. </body>
  45. </html>

Creating the Main Function

This code contains the main function of the application. This code will login the user account when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as insert.php.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['login'])){
  5. if($_POST['username'] != "" || $_POST['password'] != ""){
  6. $username = $_POST['username'];
  7. $password = $_POST['password'];
  8. $sql = "SELECT * FROM `user` WHERE `username`=? AND `password`=? ";
  9. $query = $conn->prepare($sql);
  10. $query->execute(array($username,$password));
  11. $row = $query->rowCount();
  12. $fetch = $query->fetch();
  13. if($row > 0) {
  14. echo"<center><h5 class='text-success'>Login successfully</h5></center>";
  15. } else{
  16. echo"<center><h5 class='text-danger'>Invalid username or password</h5></center>";
  17. }
  18. }
  19. }
  20. ?>

There you have it we successfully created a Easy Login Using PDO using PDO. 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 for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


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

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