Easy login script without database

Easy login script without database

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Hardcoded Credentials Are Not an Option

The request for a login script without a database often stems from a desire for simplicity or a misunderstanding of how authentication actually works. The proposed solution uses PHP to store credentials directly in the code: $ID = "admin"; and $pass = "123456";. While this approach technically fulfills the requirement of not using a database, it completely fails the safety requirement. Hardcoding usernames and passwords into source files is universally considered a critical security vulnerability in web development.

When you store credentials directly in your script, anyone who can view the source code—whether through browser inspection, server file access, or a malicious update to your site—immediately gains access to your admin account. This exposes your application to credential theft without any need for advanced attack vectors. Furthermore, this method offers zero scalability. If you decide later that you want two different administrators with different passwords, you are forced to modify the source code on the server and redeploy the entire application. In a production environment, this is an unacceptable operational risk.

The Flaw in Session Handling

Beyond the hardcoded credentials, the provided PHP snippet contains several logical errors that would prevent it from functioning correctly even if the password wasn't hardcoded.

First, there is a variable mismatch in the authentication logic. The code initializes $ID as "admin" but checks against $anvandarID. Unless $anvandarID is defined elsewhere or intended to be $_POST["ID"], this condition will never evaluate to true, and no user will ever log in. In a working script, you would compare the posted value directly:

if ($_POST["ID"] === $ID && $_POST["pass"] === $pass) { 
    // ...
}

Second, the session variable used for checking login status is inconsistent. The login script sets $_SESSION["inloggedin"], but the protected page checks for $_SESSION["inloggning"]. While PHP sessions are stored on the server and can technically handle different keys, this inconsistency leads to logic errors where a user might successfully authenticate but still be redirected to the login page because the session key does not match what the gatekeeper expects.

The protected route in safe_site.php also uses an undefined variable $anvandarID in the source text provided, or assumes the session key exists when it might not have been set correctly due to the mismatch above. A robust implementation must ensure that the session flag used for protection is strictly defined during the login process and consistently checked during route authorization.

The Real Solution: Hashing and State Storage

To build a safe login script without a database, you must separate storage from runtime logic. Even if you refuse to use a relational database like MySQL or PostgreSQL, you can store sensitive data in server-side files using file_put_contents to write hashed passwords to a secure configuration file that is not publicly readable. However, the industry standard—and the only way to achieve true safety—is to use a database.

Modern PHP frameworks abstract this complexity away. If your goal is rapid development with built-in security features, including secure password hashing, CSRF protection, and session management, using a framework like Laravel is advisable. The laravelcompany.com resource provides comprehensive documentation on building secure applications where the authentication layer handles these complexities automatically.

If you strictly cannot use a database, you are forced to implement file-based storage for user data. This requires careful handling of file permissions (e.g., ensuring the file is not world-readable) and implementing password hashing using password_hash() and password_verify(). Never store plaintext passwords. The provided script stores "123456" in plain text, which is the definition of a security failure.

Conclusion on Architecture

Building an authentication system without a database is rarely a sound architectural decision for anything beyond a static, single-user demo. It creates technical debt that makes future maintenance difficult and introduces significant security risks that cannot be easily patched. The trade-off between "no database" and "secure" is usually false; achieving one without the other results in an insecure application. For any production use case, integrating a lightweight database or switching to a framework with built-in ORM capabilities is the only path to a secure and maintainable login system.

Our network