Building a Simple No-Database Login System Using PHP

In web development, implementing a login system is a fundamental task. While using databases for authentication is standard practice, there are scenarios where a lightweight, no-database solution might be more suitable. In this post, we'll explore how to create a no-database login system using PHP, and compare its pros and cons with a similar system built using JavaScript, as discussed in the blog Building a Simple No-Database Login System Using JavaScript.

Introduction

A no-database login system can be handy for small projects, prototypes, or personal applications where simplicity and quick setup are key. We'll create a simple PHP login system that uses hardcoded user credentials and PHP sessions to manage the login state.

PHP No-Database Login System

Code Explanation

Step 1: Starting the Session

<?php
session_start();
?>
        

This starts a session or resumes the current one based on a session identifier passed via a GET or POST request, or cookies. Sessions are used to store data across multiple pages.

Step 2: Storing User Credentials

$users = [
    'username1' => 'password1',
    'username2' => 'password2',
];
        

This array stores the usernames and passwords. For simplicity, the credentials are hardcoded.

Step 3: Handling Form Submission

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (isset($users[$username]) && $users[$username] == $password) {
        $_SESSION['loggedin'] = true;
        $_SESSION['username'] = $username;
        header('Location: welcome.php');
        exit;
    } else {
        $error = 'Invalid username or password';
    }
}
        

This checks if the form has been submitted using the POST method, retrieves the username and password from the form, and validates them against the array. If the credentials are correct, it sets session variables and redirects to the welcome page.

Step 4: Displaying the Login Form

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <form method="POST" action="">
        Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
        <input type="submit" value="Login">
    </form>
    <?php if (isset($error)) { echo $error; } ?>
</body>
</html>
        

This simple HTML form collects the user's username and password. If there is an error message set, it is displayed below the form.

Step 5: Creating the Welcome Page

<?php
session_start();

if (!isset($_SESSION['loggedin'])) {
    header('Location: index.php');
    exit;
}

echo 'Welcome, ' . $_SESSION['username'] . '!';
?>
        

The welcome page starts the session and checks if the user is logged in by verifying the session variable. If not, it redirects to the login page. If logged in, it displays a welcome message with the username.

Enhancing Security

To improve security, consider hashing passwords and using a separate configuration file for user credentials. Here's an enhanced version with password hashing:

Storing Hashed Passwords

$users = [
    'username1' => password_hash('password1', PASSWORD_DEFAULT),
    'username2' => password_hash('password2', PASSWORD_DEFAULT),
];
        

Verifying Passwords

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (isset($users[$username]) && password_verify($password, $users[$username])) {
        $_SESSION['loggedin'] = true;
        $_SESSION['username'] = $username;
        header('Location: welcome.php');
        exit;
    } else {
        $error = 'Invalid username or password';
    }
}
        

Using password_hash() and password_verify() enhances security by storing hashed passwords and verifying them securely.

Pros and Cons

PHP No-Database Login System

Pros:

  • Simplicity: Easy to implement and understand.
  • No Database Required: Quick setup without a database.
  • Speed: Faster as it doesn't involve database queries.
  • Portability: Easily transferable between environments.

Cons:

  • Scalability: Not suitable for many users.
  • Security: Storing plain text passwords is insecure.
  • Maintenance: Updating credentials requires manual script editing.
  • Limited Functionality: Lacks advanced features like password hashing, user roles, and account management.

JavaScript No-Database Login System

From the blog Building a Simple No-Database Login System Using JavaScript:

Pros:

  • Client-Side Execution: Runs entirely on the client's browser, reducing server load.
  • Quick Feedback: Users receive immediate feedback without the need for server communication.
  • Simple Setup: Easy to set up without server-side coding.
  • Good for Prototyping: Ideal for quick prototypes or demos.

Cons:

  • Security: Client-side scripts are easily viewable and modifiable, making it highly insecure.
  • No Persistent Sessions: Lacks session management, leading to poor security and user experience.
  • Limited Functionality: Not suitable for production use due to lack of server-side validation.
  • Scalability: Not suitable for applications with many users.

Comparison

Security:

  • PHP: More secure as it runs on the server and can use sessions.
  • JavaScript: Less secure because it runs on the client and is easily accessible.

Scalability:

  • PHP: Slightly better scalability since it can be adapted to use databases later.
  • JavaScript: Poor scalability due to client-side limitations.

Setup and Maintenance:

  • PHP: Requires some server-side setup and maintenance.
  • JavaScript: Simple setup but limited to client-side, making maintenance tricky for secure applications.

User Experience:

  • PHP: Better user experience with session management and server-side processing.
  • JavaScript: Immediate feedback but lacks session management and security.

Conclusion

Creating a no-database login system in PHP is a simple and quick solution for small-scale applications. While it has limitations, understanding this basic implementation is a valuable step in learning web development. For more complex applications, consider using databases and implementing additional security measures.

Feel free to check out the detailed JavaScript no-database login system in the blog Building a Simple No-Database Login System Using JavaScript for more insights!

0 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


You may like to read


Follow Us

Sponsored Ads

GranNino Ads

Newsletter

Subscribe to our Newsletter to read our latest posts at first

We would not spam your inbox! Promise
Get In Touch

© Fullstack Coding Tips. All Rights Reserved.