Creating a Login and Registration form in PHP and SQLite using PDO

In this tutorial, we will create a Login And Registration To SQLite Using PDO. This code can be used for storing the list of data. 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 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 PDO in our PHP.

  1. Open localhost server folder XAMPP, etc and locate php.ini.
  2. Open php.ini and enable pdo_sqlite by removing the semicolon in the line.
    tut1
  3. Save the 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. //check if the database file exists and create a new if not
  3. if(!is_file('db/db_member.sqlite3')){
  4. file_put_contents('db/db_member.sqlite3', null);
  5. }
  6. // connecting the database
  7. $conn = new PDO('sqlite:db/db_member.sqlite3');
  8. //Setting connection attributes
  9. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. //Query for creating reating the member table in the database if not exist yet.
  11. $query = "CREATE TABLE IF NOT EXISTS member(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT, firstname TEXT, lastname TEXT)";
  12. //Executing the query
  13. $conn->exec($query);
  14. ?>

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
  3. //starting the session
  4. ?>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  8. <!-- Bootstrap -->
  9. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  10. </head>
  11. <body>
  12. <nav class="navbar navbar-default">
  13. <div class="container-fluid">
  14. <a class="navbar-brand" href="https://sourcecodester.com" target="_balnk">Sourcecodester</a>
  15. </div>
  16. </nav>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6 well">
  19. <h3 class="text-primary">PHP - Login And Registration To Sqlite Using PDO</h3>
  20. <hr style="border-top:1px dotted #ccc;"/>
  21. <!-- Link for redirecting to Login Page -->
  22. <a href="login.php">Already a member? Log in here...</a>
  23. <br style="clear:both;"/><br />
  24. <div class="col-md-3"></div>
  25. <div class="col-md-6">
  26. <!-- Registration Form start -->
  27. <form method="POST" action="save_member.php">
  28. <div class="alert alert-info">Registration</div>
  29. <div class="form-group">
  30. <label>Username</label>
  31. <input type="text" name="username" class="form-control" required="required"/>
  32. </div>
  33. <div class="form-group">
  34. <label>Password</label>
  35. <input type="password" name="password" class="form-control" required="required"/>
  36. </div>
  37. <div class="form-group">
  38. <label>Firstname</label>
  39. <input type="text" name="firstname" class="form-control" required="required"/>
  40. </div>
  41. <div class="form-group">
  42. <label>Lastname</label>
  43. <input type="text" name="lastname" class="form-control" required="required"/>
  44. </div>
  45. <?php
  46. //checking if the session 'success' is set.
  47. if(ISSET($_SESSION['success'])){
  48. ?>
  49. <!-- Display regostration success message -->
  50. <div class="alert alert-success"><?php echo $_SESSION['success']?></div>
  51. <?php
  52. //Unsetting the 'success' session after displaying the message.
  53. unset($_SESSION['success']);
  54. }
  55. ?>
  56. <button class="btn btn-primary btn-block" name="register"><span class="glyphicon glyphicon-save"></span> Register</button>
  57. </form>
  58. <!-- Registration Form end -->
  59. </div>
  60. </div>
  61. </body>
  62. </html>
login.php
  1. <!DOCTYPE html>
  2. <?php
  3. //starting the session
  4. ?>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  8. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  9. </head>
  10. <body>
  11. <nav class="navbar navbar-default">
  12. <div class="container-fluid">
  13. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  14. </div>
  15. </nav>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6 well">
  18. <h3 class="text-primary">PHP - Login And Registration To Sqlite Using PDO</h3>
  19. <hr style="border-top:1px dotted #ccc;"/>
  20. <!-- Link for redirecting page to Registration page -->
  21. <a href="index.php">Not a member yet? Register here...</a>
  22. <br style="clear:both;"/><br />
  23. <div class="col-md-3"></div>
  24. <div class="col-md-6">
  25. <!-- Login Form Starts -->
  26. <form method="POST" action="login_query.php">
  27. <div class="alert alert-info">Login</div>
  28. <div class="form-group">
  29. <label>Username</label>
  30. <input type="text" name="username" class="form-control" required="required"/>
  31. </div>
  32. <div class="form-group">
  33. <label>Password</label>
  34. <input type="password" name="password" class="form-control" required="required"/>
  35. </div>
  36. <?php
  37. //checking if the session 'error' is set. Erro session is the message if the 'Username' and 'Password' is not valid.
  38. if(ISSET($_SESSION['error'])){
  39. ?>
  40. <!-- Display Login Error message -->
  41. <div class="alert alert-danger"><?php echo $_SESSION['error']?></div>
  42. <?php
  43. //Unsetting the 'error' session after displaying the message.
  44. unset($_SESSION['error']);
  45. }
  46. ?>
  47. <button class="btn btn-primary btn-block" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  48. </form>
  49. <!-- Login Form Ends -->
  50. </div>
  51. </div>
  52. </body>
  53. </html>
home.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. <!-- Bootstrap -->
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com" target="_blank">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 And Registration To Sqlite Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <a href="login.php">Logout</a>
  18. <h1>Welcome User!</h1>
  19. </div>
  20. </body>
  21. </html>

Creating the Main Function

This code contains the main function of the application. This code is consists of two functionalities, the registration, and the login. To do this just copy and write these blocks of codes as shown below inside the text editor, then save it as shown below.

save_member.php
  1. <?php
  2. //starting the session
  3.  
  4. //including the database connection
  5. require_once 'conn.php';
  6.  
  7. if(ISSET($_POST['register'])){
  8. // Setting variables
  9. $username = $_POST['username'];
  10. $password = $_POST['password'];
  11. $firstname = $_POST['firstname'];
  12. $lastname = $_POST['lastname'];
  13.  
  14. // Insertion Query
  15. $query = "INSERT INTO `member` (username, password, firstname, lastname) VALUES(:username, :password, :firstname, :lastname)";
  16. $stmt = $conn->prepare($query);
  17. $stmt->bindParam(':username', $username);
  18. $stmt->bindParam(':password', $password);
  19. $stmt->bindParam(':firstname', $firstname);
  20. $stmt->bindParam(':lastname', $lastname);
  21.  
  22. // Check if the execution of query is success
  23. if($stmt->execute()){
  24. //setting a 'success' session to save our insertion success message.
  25. $_SESSION['success'] = "Successfully created an account";
  26.  
  27. //redirecting to the index.php
  28. header('location: index.php');
  29. }
  30.  
  31. }
  32. ?>
login_query.php
  1. <?php
  2. //starting the session
  3. //including the database connection
  4. require_once 'conn.php';
  5.  
  6. if(ISSET($_POST['login'])){
  7. // Setting variables
  8. $username = $_POST['username'];
  9. $password = $_POST['password'];
  10.  
  11. // Select Query for counting the row that has the same value of the given username and password. This query is for checking if the access is valid or not.
  12. $query = "SELECT COUNT(*) as count FROM `member` WHERE `username` = :username AND `password` = :password";
  13. $stmt = $conn->prepare($query);
  14. $stmt->bindParam(':username', $username);
  15. $stmt->bindParam(':password', $password);
  16. $stmt->execute();
  17. $row = $stmt->fetch();
  18.  
  19. $count = $row['count'];
  20. if($count > 0){
  21. header('location:home.php');
  22. }else{
  23. $_SESSION['error'] = "Invalid username or password";
  24. header('location:login.php');
  25. }
  26. }
  27. ?>

There you have it we successfully created a Login And Registration 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!!!

Comments

Submitted byoleteacheron Sat, 12/15/2018 - 10:16

Thank you for sharing this code. Using in classroom project. Since we cannot use mySQL, the sqlite projects are wonderful to have in classroom.

Add new comment