PHP - SQLite Login App

In this tutorial we will create a SQLite Login App using PHP. This code will login the user account when user click the login button in the table. The code use PHP POST method to call a specific function that will login the user account using SELECT query and adding condition in the WHERE clause which the value is assign by an id. This a free program, you can apply this to your system as your own. We will be using SQLite that 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.

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 SQLite3('db/db_user') or die("Unable to open database!");
  3.         $query="CREATE TABLE IF NOT EXISTS `user`(user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)";
  4.        
  5.         $conn->exec($query);
  6. ?>

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 index.php.
  1. <!DOCTYPE html>
  2. <?php require_once 'conn.php'?>
  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 - SQLite Login App</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-3">
  19.                         <h6>Note:</h6>
  20.                         <h5>Username: admin</h5>
  21.                         <h5>Password: admin</h5>
  22.                 </div>
  23.                 <div class="col-md-5">
  24.                         <form method="POST" action="">
  25.                                 <div class="form-group">
  26.                                         <label>Username</label>
  27.                                         <input type="text" class="form-control" name="username" required="required"/>
  28.                                 </div>
  29.                                 <div class="form-group">
  30.                                         <label>Password</label>
  31.                                         <input type="text" class="form-control" name="password" required="required"/>
  32.                                 </div>
  33.                                 <center><button class="btn btn-primary" name="login">Login</button></center>
  34.                         </form>
  35.                        
  36.                         <?php include'login.php'?>
  37.                 </div>
  38.         </div>
  39. </body>
  40. </html>

Creating the Main Function

This code contains the main function of the application. This code will login the given user account when the form is submitted. To do this just copy and write these block of codes as shown below inside the text editor, then save it as login.php.
  1. <?php
  2.         require_once'conn.php';
  3.         if(ISSET($_POST['login'])){
  4.                 $username=$_POST['username'];
  5.                 $password=$_POST['password'];
  6.                
  7.                 $query=$conn->query("SELECT COUNT(*) as count from `user` WHERE `username`='$username' AND `password`='$password'");
  8.                
  9.                
  10.                 $result=$query->fetchArray();
  11.                
  12.                 $row=$result['count'];
  13.                
  14.                 if($row>0){
  15.                         echo"<center><h5 class='text-success'>Successfully login!</h5></center>";
  16.                 }else{
  17.                         echo"<center><h5 class='text-danger'>Invalid username or password</h5></center>";
  18.                 }
  19.                
  20.         }
  21. ?>
There you have it we successfully created a SQLite Login App using PHP. 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