PHP - Login And Registration To Sqlite Using PDO

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

In this tutorial we will create a Login And Registration To Sqlite Using PDO. This code can be use for storing of 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 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_member.sqlite3');
  3.  
  4. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5.  
  6. $query = "CREATE TABLE IF NOT EXISTS member(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT, firstname TEXT, lastname TEXT)";
  7.  
  8. $conn->exec($query);
  9. ?>

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 - Login And Registration To Sqlite Using PDO</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <a href="login.php">Already a member? Log in here...</a>
  19. <br style="clear:both;"/><br />
  20. <div class="col-md-3"></div>
  21. <div class="col-md-6">
  22. <form method="POST" action="save_member.php">
  23. <div class="alert alert-info">Registration</div>
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" name="password" class="form-control" required="required"/>
  31. </div>
  32. <div class="form-group">
  33. <label>Firstname</label>
  34. <input type="text" name="firstname" class="form-control" required="required"/>
  35. </div>
  36. <div class="form-group">
  37. <label>Lastname</label>
  38. <input type="text" name="lastname" class="form-control" required="required"/>
  39. </div>
  40. <?php
  41. if(ISSET($_SESSION['success'])){
  42. ?>
  43. <div class="alert alert-success"><?php echo $_SESSION['success']?></div>
  44. <?php
  45. session_unset($_SESSION['success']);
  46. }
  47. ?>
  48. <button class="btn btn-primary btn-block" name="register"><span class="glyphicon glyphicon-save"></span> Register</button>
  49. </form>
  50. </div>
  51. </div>
  52. </body>
  53. </html>

login.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 - Login And Registration To Sqlite Using PDO</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <a href="index.php">Not a member yet? Register here...</a>
  19. <br style="clear:both;"/><br />
  20. <div class="col-md-3"></div>
  21. <div class="col-md-6">
  22. <form method="POST" action="login_query.php">
  23. <div class="alert alert-info">Login</div>
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" name="password" class="form-control" required="required"/>
  31. </div>
  32. <?php
  33. if(ISSET($_SESSION['error'])){
  34. ?>
  35. <div class="alert alert-danger"><?php echo $_SESSION['error']?></div>
  36. <?php
  37. session_unset($_SESSION['error']);
  38. }
  39. ?>
  40. <button class="btn btn-primary btn-block" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  41. </form>
  42. </div>
  43. </div>
  44. </body>
  45. </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. <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 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 consist of two functionalities, the registration and the login. To do this just copy and write these block of codes as shown below inside the text editor, then save it as shown below.

save_member.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['register'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7. $firstname = $_POST['firstname'];
  8. $lastname = $_POST['lastname'];
  9.  
  10. $query = "INSERT INTO `member` (username, password, firstname, lastname) VALUES(:username, :password, :firstname, :lastname)";
  11. $stmt = $conn->prepare($query);
  12. $stmt->bindParam(':username', $username);
  13. $stmt->bindParam(':password', $password);
  14. $stmt->bindParam(':firstname', $firstname);
  15. $stmt->bindParam(':lastname', $lastname);
  16.  
  17. if($stmt->execute()){
  18. $_SESSION['success'] = "Successfully created an account";
  19. header('location: index.php');
  20. }
  21.  
  22. }
  23. ?>

login_query.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 `member` 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:login.php');
  22. }
  23. }
  24. ?>

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

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

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.