PHP - Simple Login Application To SQLite Using PDO

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