PHP - Simple Login Application To SQLite Using PDO

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

In this tutorial we will create a Simple Login Application To SQLite Using PDO. PHP is a server-side scripting language designed primarily for web development. SQLite provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP. 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/.

Installing SQLite Browser
We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.

Setting Up SQLite

First, we are going to enable SQLite 3 in our PHP.

1. Open localhost server folder XAMPP, etc and locate php.ini.
2. Open php.ini and enable sqlite3 by removing the semicolon in the line.
tut1
3. Save changes and Restart Server.

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('sqlite:db/db_login.sqlite3');
  3.  
  4. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5. $conn->exec("CREATE TABLE IF NOT EXISTS user (user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)");
  6.  
  7. $query = $conn->prepare("SELECT COUNT(*) as count FROM `user`");
  8. $query->execute();
  9. $row = $query->fetch();
  10.  
  11. $count = $row['count'];
  12.  
  13. if($count == 0){
  14. $query = "INSERT INTO `user` (username, password) VALUES(:username, :password)";
  15. $stmt = $conn->prepare($query);
  16. $stmt->bindValue(':username', 'admin');
  17. $stmt->bindValue(':password', 'admin');
  18. $stmt->execute();
  19. }
  20. ?>

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. <?php session_start()?>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </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 Application To SQLite 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. <h3 class="text-info">Login</h3>
  21. <hr style="border-top:1px solid #000;"/>
  22. <form method="POST" action="login.php">
  23. <div class="form-group">
  24. <label>Username</label>
  25. <input type="text" name="username" class="form-control" required="required"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Password</label>
  29. <input type="password" name="password" class="form-control" required="required"/>
  30. </div>
  31. <?php
  32. if(ISSET($_SESSION['error'])){
  33. ?>
  34. <div class="alert alert-danger"><?php echo $_SESSION['error']?></div>
  35. <?php
  36. session_unset($_SESSION['error']);
  37. }
  38. ?>
  39. <button name="login" class="btn btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  40. </form>
  41. </div>
  42. </div>
  43. </body>
  44. </html>

home.php

  1. <!DOCTYPE html>
  2. <?php session_start()?>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </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 Application To SQLite 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. <a class="btn btn-success" href="index.php">Back</a>
  21. <h2>Successfully Login!</h2>
  22. </div>
  23. </div>
  24. </body>
  25. </html>

Creating the Main Function

This code contains the main function of the application. This code will process and validate the user data input to check if user is allowed to login To do this just copy and write these block of codes as shown below inside the text editor and save it as login.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['login'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7.  
  8. $query = "SELECT COUNT(*) as count FROM `user` WHERE `username` = :username AND `password` = :password";
  9. $stmt = $conn->prepare($query);
  10. $stmt->bindParam(':username', $username);
  11. $stmt->bindParam(':password', $password);
  12. $stmt->execute();
  13. $row = $stmt->fetch();
  14.  
  15. $count = $row['count'];
  16.  
  17. if($count > 0){
  18. header('location:home.php');
  19. }else{
  20. $_SESSION['error'] = "Invalid username or password";
  21. header('location:index.php');
  22. }
  23. }
  24. ?>

There you have it we successfully created a Simple Login Application To SQLite 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.