PHP - Simple Login Using PDO

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

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="<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 Using PDO</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <form method="post" action="login.php">
  21. <div class="form-group">
  22. <label>Username</label>
  23. <input type="text" name="username" class="form-control"/>
  24. </div>
  25. <div class="form-group">
  26. <label>Password</label>
  27. <input type="password" name="password" class="form-control"/>
  28. </div>
  29.  
  30. <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  31. </form>
  32. </div>
  33. </div>
  34. </body>
  35. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  16. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  17. </a> </div>
  18. </nav>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6 well">
  21. <h3 class="text-primary">PHP - Simple Login Using PDO</h3>
  22. <hr style="border-top:1px dotted #ccc;"/>
  23. <div class="col-md-3"></div>
  24. <div class="col-md-6">
  25. <center><h2>This is Home Page</h2></center>
  26. <center><a href="logout.php">Logout</a></center>
  27. </div>
  28. </div>
  29. </body>
  30. </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

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.