PHP - Easy Login Using PDO

Submitted by: 
Visitors have accessed this post 402 times.

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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.