Hardcoding Credentials vs. Proper Session Handling in a Static PHP Login
Attempting to build a login system without a database often stems from a desire for simplicity, but the code provided reveals several critical issues that prevent authentication from working as intended. The primary failure point lies in how variables are captured and compared within trylog.php, combined with a misunderstanding of when session state should be checked versus when raw input validation occurs.
In the provided script, the variable assignment $username = '$_POST[username]'; is syntactically incorrect. In PHP, you cannot nest quotes like that to access an array index. The correct syntax requires concatenation or direct indexing without the outer single quotes: $username = $_POST['username'];. Failing to fix this results in a notice (or error depending on configuration) and leaves $username as a literal string containing $_POST[username], ensuring it will never match the hardcoded $usr variable.
Beyond syntax, there is a logical flaw in the authentication flow. The script attempts to check $_SESSION['login'] immediately after starting the session, but this happens before any validation logic has actually set that flag based on current input. While the condition includes an else block for hardcoded credentials, mixing session state checks with direct POST comparison in a single if statement can lead to confusing behavior, especially if a user refreshes the page after logging in. A cleaner approach validates the input first, sets the session variable only upon success, and then redirects or renders the protected content.
Implementing Hardcoded Authentication Safely
If the requirement is strictly for a single user with no database storage, you can hardcode the credentials directly into the PHP file. However, this should be treated as a prototype or an internal tool, never production software for sensitive data. Here is a corrected version of trylog.php that addresses the syntax errors and improves the logic:
<?php
session_start();
// Define hardcoded credentials
$expected_username = 'admin';
$expected_password = 'password';
// Capture POST data safely
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
if ($username === $expected_username && $password === $expected_password) {
// Only set session if login is successful
$_SESSION['logged_in'] = true;
// Clear POST data to prevent re-submission attacks (CSRF mitigation basic step)
unset($_POST);
echo "Login successful. Redirecting...";
// In a real app, use header() to redirect immediately
// header('Location: adminportal.php');
// exit;
} else {
echo "Incorrect username or password.";
}
?>
Security Considerations for Static Credentials
Hardcoding passwords directly in source code introduces significant security risks. Anyone with read access to the server files can locate the login.php or trylog.php script and view the plaintext credentials. Furthermore, this method offers no protection against credential stuffing attacks; if an attacker discovers the hardcoded password, they have persistent access until you manually edit the file.
A more robust alternative for a "simple" login without a database is to store the hash of the password in a configuration file (like .env) rather than plain text in the main script, though this still requires the application to know where that file lives. For even better security with just one user, consider using a lightweight database like SQLite or a key-value store like Redis, which eliminates the need for manual credential management while maintaining simplicity.
Improving the Form and HTML
The original login.php form also contains several minor but important corrections needed. The input type for the username field is incorrectly set to "username". Standard HTML forms require type="text" for text inputs. Additionally, passwords should never be submitted as plain text in modern applications; while this specific example hardcodes the password for comparison, a production system must always hash stored credentials and verify using hashed comparisons (e.g., using password_verify).
The provided snippet also includes an "Add another Article" form header, which suggests this file might be part of a larger content management interface. If this is indeed a CMS prototype, ensuring that the login logic correctly identifies the user before allowing access to content creation features is vital. The adminportal.php link in the original code uses method="get" for the home button action, which is acceptable for navigation, but the initial login must happen over POST to ensure credentials are not leaked into server logs or browser history.
Next Steps
Once the syntax errors are resolved and the logic flow is verified, the application will function as a simple gatekeeper. However, remember that this architecture lacks scalability. As soon as you need to support multiple users, reset passwords, or track login attempts, moving to a database-backed solution becomes mandatory. For developers looking to scale beyond static files quickly, frameworks like Laravel provide robust authentication scaffolding out of the box, ensuring security best practices are met from day one.