PHP SQLite3 User Login Example

Here is a simple PHP SQLite3 User Login example.

First create our database called Login.db in SQLite3.

CREATE TABLE "users" (
	"id"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"username"	TEXT,
	"password"	TEXT
)

You will want to insert sample data in that database.

INSERT INTO "users" (username,password) VALUES ('test_username','test_password');

Let’s create our HTML form.

...
<form action="login.php" method="post">
Username:<input type="text" name="username"/><br/>
Password:<input type="password" name="password"/><br/>
<input type="submit"/>
</form>
...

Login.php

<?php

$username= $_POST['username'];
$password= $_POST['password'];

$db = new SQLite3('login.db');
$statement = $db->prepare('SELECT count(*) FROM users WHERE username=:username AND password=:password');
$statement->bindValue(':username', $username, SQLITE3_TEXT);
$statement->bindValue(':password', $password, SQLITE3_TEXT);
$result = $statement->execute();

while ($row= $result->fetchArray()) {
    $count = $row['count(*)'];
}

if ($count>0) {
   echo "Success!";
} 
else {
   echo "Failed to Login!";
}