PHP - Simple Login Using PDO

In this tutorial we will create a Simple Login Using PDO. PHP is a server-side scripting language designed primarily for web development. PDO stands for PHP Data Objects. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

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_pdologin, 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(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new PDO('mysql:host=localhost;dbname=db_pdologin', 'root', '');
  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 you 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 Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <form method="post" action="login.php">
  20. <div class="form-group">
  21. <label>Username</label>
  22. <input type="text" name="username" class="form-control"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Password</label>
  26. <input type="password" name="password" class="form-control"/>
  27. </div>
  28.  
  29. <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  30. </form>
  31. </div>
  32. </div>
  33. </body>
  34. </html>
home.php
  1. <?php
  2. if(!ISSET($_SESSION['user'])){
  3. header('location: index.php');
  4. }
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  10. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  11. </head>
  12. <body>
  13. <nav class="navbar navbar-default">
  14. <div class="container-fluid">
  15. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  16. </div>
  17. </nav>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6 well">
  20. <h3 class="text-primary">PHP - Simple Login Using PDO</h3>
  21. <hr style="border-top:1px dotted #ccc;"/>
  22. <div class="col-md-3"></div>
  23. <div class="col-md-6">
  24. <center><h2>This is Home Page</h2></center>
  25. <center><a href="logout.php">Logout</a></center>
  26. </div>
  27. </div>
  28. </body>
  29. </html>

Creating the Main Function

This code contains the main function of the application. This can login the user account and logout at the same time. To do that write these block of codes inside the Text editor and save it accordingly as shown below. logout.php
  1. <?php
  2. unset($_SESSION['user']);
  3. header('location: index.php');
  4. ?>
login.php
  1. <?php
  2.  
  3. require_once 'conn.php';
  4.  
  5. if(ISSET($_POST['login'])){
  6. if($_POST['username'] != "" || $_POST['password'] != ""){
  7. $username = $_POST['username'];
  8. $password = $_POST['password'];
  9. $sql = "SELECT * FROM `user` WHERE `username`=? AND `password`=? ";
  10. $query = $conn->prepare($sql);
  11. $query->execute(array($username,$password));
  12. $row = $query->rowCount();
  13. $fetch = $query->fetch();
  14. if($row > 0) {
  15. $_SESSION['user'] = $fetch['user_id'];
  16. header("location: home.php");
  17. } else{
  18. echo "
  19. <script>alert('Invalid username or password')</script>
  20. <script>window.location = 'index.php'</script>
  21. ";
  22. }
  23. }else{
  24. echo "
  25. <script>alert('Please complete the required field!')</script>
  26. <script>window.location = 'index.php'</script>
  27. ";
  28. }
  29. }
  30. ?>
There you have it we successfully created a Simple Login 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!!!

Add new comment