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!

267 11

11 Comments

Brahman WebTech Fullstack Tips
Brahman WebTech Fullstack Tips 2025-12-10 05:17:32

I appreciate your suggestion about moving to .NET, but I have to be honest — I remain strongly biased toward PHP. The reasons are simple: Cost efficiency: PHP solutions, especially with WordPress, are far more affordable compared to .NET hosting and licensing. Flexibility: WordPress and PHP have a massive ecosystem of plugins, themes, and integrations that make customization easy without reinventing the wheel. Migration concerns: I’ve already invested time and effort into WordPress. Switching platforms like BlogEngine.NET would mean complex migration, potential data loss, and retraining. While BlogEngine.NET does have import options, it’s not as seamless as staying within the PHP/WordPress ecosystem. Community support: PHP and WordPress have one of the largest developer communities worldwide, which means faster troubleshooting, more tutorials, and better long‑term support. Instead of moving to .NET, I’d prefer to continue leveraging PHP and WordPress. That’s where Brahman WebTech (bmwtech.in) comes in. Hosting, development, and advanced features — at a flat cost of just $500 for my personal blogging site. This package even includes: Reliable hosting optimized for WordPress/PHP. Payment gateway integration for monetization or subscriptions. Custom features tailored to my blogging needs. End‑to‑end support so you don’t have to worry about technical headaches. With this setup, you get the affordability and flexibility of PHP without sacrificing functionality. So while I respect your enthusiasm for .NET, I’m committed to PHP and WordPress - and with Brahman WebTech’s support, I can achieve everything I need at a fraction of the cost.

Kassie Bolick
Kassie Bolick 2025-12-07 09:45:58

My developer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on several websites for about a year and am nervous about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can import all my wordpress content into it? Any help would be greatly appreciated!

Shonda Astley
Shonda Astley 2025-12-03 04:23:11

I always used to study post in news papers but now as I am a user of net therefore from now I am using net for posts, thanks to web.

Gennie Aragon
Gennie Aragon 2025-07-19 09:54:13

Nice post. I was checking continuously this blog and I'm impressed! Extremely useful info particularly the closing phase :) I maintain such info much. I used to be seeking this certain information for a long time. Thank you and best of luck.

Beverly Willason
Beverly Willason 2025-06-28 06:15:27

I every time emailed this weblog post page to all my associates, as if like to read it then my friends will too.

Shella Lawton
Shella Lawton 2025-06-14 01:04:08

I don't even know how I ended up here, but I thought this post was great. I don't know who you are but definitely you are going to a famous blogger if you aren't already ;) Cheers!

Saul Bard
Saul Bard 2025-06-09 08:37:55

I am really impressed ѡith your writing skills aѕ well aѕ witһ the layout on your blog. Is this ɑ paid theme or Ԁid you customize it youself? Аnyway keep up the excellent quazlity writing, іt is rare to see a ցreat blog liкe this onne thesе daуs.

Sheila Forte
Sheila Forte 2025-06-06 11:06:12

Greetings! Very useful advice within this article! It is the little changes that will make the most important changes. Thanks for sharing!

Mozelle Melros
Mozelle Melros 2025-05-04 13:13:40

Great Tutorial, But I am looking for Database Login System with PHP for my custom Wordpress site. Is there any tutorial here in this blog?

Perry Wylie
Perry Wylie 2025-04-27 23:40:09

I have read this post and if I could I want to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I desire to read more things about it!

Floy Stein
Floy Stein 2025-04-26 05:20:06

I'm now not sure where you are getting your information, but good topic. I must spend a while finding out much more or figuring out more. Thanks for fantastic information I was searching for this info for my mission.

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.