<?php
$my_conn = new PDO('sqlite:'.dirname(__FILE__).'/plus2net_users.db'); // Inside the same directory
try {
$count=$my_conn->prepare("
CREATE TABLE users (
userid TEXT NOT NULL,
password TEXT NOT NULL,
email TEXT NOT NULL);
");
if($count->execute()){
echo "TABLE users created";
}else{
echo "Not able to create table users";
}
// Insert a sample user record
$count=$my_conn->exec("INSERT INTO users (userid, password, email)
VALUES ('user1', 'password123', 'user1@example.com')");
echo "<BR>Number of records added: ".$count;
// Close the connection
$my_conn = null;
}
catch(PDOException $e)
{
// Print PDOException message
echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-4">
<h2 class="text-center mt-5">Login</h2>
<form action="check-login.php" method="POST">
<div class="form-group mb-3">
<label for="userid">User ID</label>
<input type="text" name="userid" id="userid" class="form-control" required>
</div>
<div class="form-group mb-3">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
</form>
</div>
</div>
</div>
<!-- Bootstrap JS and Popper.js -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php
session_start(); // Start the session
try {
// Connect to SQLite database
$my_conn = new PDO('sqlite:' . dirname(__FILE__) . '/plus2net_users.db'); // Inside the same directory
// Get the form data
$userid = $_POST['userid'];
$password = $_POST['password'];
// Prepare the SQL statement to check for the user
$stmt = $my_conn->prepare("SELECT * FROM users WHERE userid = :userid AND password = :password");
// Bind parameters
$stmt->bindParam(':userid', $userid);
$stmt->bindParam(':password', $password); // Assuming password is stored as plain text for this example. Use hashing in production.
$stmt->execute(); // Execute the query
$user = $stmt->fetch(PDO::FETCH_ASSOC); // Fetch the result
// Check if a matching user was found
if ($user) {
// User found, create session
$_SESSION['userid'] = $user['userid'];
$_SESSION['email'] = $user['email'];
echo "<h2>Welcome, " . htmlspecialchars($user['userid']) . "!</h2>";
} else {
// Invalid credentials
echo "<h2>Login Failed. Invalid User ID or Password.</h2>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.