PHP - Login Form In SQLite

In this tutorial we will create a Login Form In SQLite using PHP. This code will login the given account in order to access the home page of the application when user click the login button. The code use a SQLite SELECT() function and by adding a WHERE clause and setting the parameter for both username and password with POST value. This is a user-friendly kind of program feel free user it in your program. 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 jquery that i used in this tutorial https://jquery.com/. Lastly, 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, name TEXT)";       
  4.         $conn->exec($query);
  5.  
  6.         $query=$conn->query("SELECT COUNT(*) as count FROM `user`");
  7.         $row=$query->fetchArray();
  8.         $countRow=$row['count'];
  9.  
  10.         if($countRow == 0){
  11.                 $conn->exec("INSERT INTO `user` (username, password, name) VALUES('admin', 'admin', 'Administrator')");
  12.         }
  13. ?>

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. <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="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Login Form In SQLite</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-6">
  18.                         <form action="" method="POST">
  19.                                 <div class="form-group">
  20.                                         <label>Username</label>
  21.                                         <input type="text" name="username" class="form-control" required="required"/>
  22.                                 </div>
  23.                                 <div class="form-group">
  24.                                         <label>Password</label>
  25.                                         <input type="password" name="password" class="form-control" required="required"/>
  26.                                 </div>
  27.                
  28.                                 <center><button name="login" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  29.                                
  30.                         </form>
  31.                         <br />
  32.                         <?php include 'login.php'?>
  33.                 </div>
  34.                 <div class="col-md-3">
  35.                         <h6>Default username: admin</h6>
  36.                         <h6>Default password: admin</h6>
  37.                 </div>
  38.         </div>
  39.  
  40. </body>
  41. </html>

Creating the Main Function

This code contains the main function of the application. This code will login the given data to SQLite database. 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.        
  4.         if(ISSET($_POST['login'])){
  5.                 $username = $_POST['username'];
  6.                 $password = $_POST['password'];
  7.                
  8.                 $query=$conn->query("SELECT COUNT(*) as count FROM `user` WHERE `username`='$username' AND `password`='$password'");
  9.                 $row=$query->fetchArray();
  10.                 $count=$row['count'];
  11.                
  12.                 if($count > 0){
  13.                         echo "<div class='alert alert-success'>Login successful</div>";
  14.                 }else{
  15.                         echo "<div class='alert alert-danger'>Invalid username or password</div>";
  16.                 }
  17.         }
  18. ?>
There you have it we successfully created a Login Form In SQLite 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!!!

Comments

Nice and simple SQLite script, thank you! If I have a folder with pages I wish to protect, is there simple way to add include at top of them and force login with this script? Really do not see how looking at example pages in download. Thanks again for sharing.

Add new comment