-
Include auth.php in the /admin Case:
Update the /admin case in your index.php to include the auth.php script. This can be done using require_once or include_once, depending on your preference. Using require_once is generally recommended to ensure the script is included only once and to halt execution if the file is not found.
case '/admin':
require_once __DIR__ . '/auth.php';
break;
Explanation:
require_once: This statement includes and evaluates the specified file during the execution of the script. If the file has already been included, it will not be included again. If the file cannot be found, it will produce a fatal error and halt the script.
__DIR__: This magic constant provides the directory of the current script, ensuring the correct path to auth.php regardless of where index.php is called from.
-
Ensure Proper Routing:
Make sure that your auth.php handles both GET and POST requests appropriately. For example, if auth.php is responsible for displaying a login form on a GET request and processing the login on a POST request, ensure that it's structured to handle these scenarios.
Example auth.php:
<?php
// auth.php
// Start session if not already started
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Check the request method
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
// Display the login form
header('Content-Type: text/html');
include __DIR__ . '/frontend/login.html';
} elseif ($method === 'POST') {
// Process the login
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
// Replace this with your actual authentication logic
if ($username === 'admin' && $password === 'password') {
$_SESSION['authenticated'] = true;
header('Location: /admin/dashboard');
exit();
} else {
echo "Invalid credentials. <a href='/admin'>Try again</a>.";
}
} else {
header("HTTP/1.0 405 Method Not Allowed");
echo "Method not allowed";
}
?>
-
Handle Authenticated Routes:
If you have additional admin routes (e.g., /admin/dashboard), ensure that you check for authentication in those routes. You can create a helper function to verify if the user is authenticated.
Example Authentication Check:
// At the beginning of index.php
function isAuthenticated() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
return isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true;
}
// Example of securing the /admin/dashboard route
case '/admin/dashboard':
if (isAuthenticated()) {
renderTemplate('admin_dashboard.html');
} else {
header('Location: /admin');
exit();
}
break;
-
Security Considerations:
- Use HTTPS: Ensure that your admin routes are served over HTTPS to protect sensitive information like usernames and passwords.
- Hash Passwords: Store hashed passwords instead of plain text in your authentication logic.
- Session Management: Implement proper session management practices, such as regenerating session IDs upon login and setting appropriate session timeouts.
- Input Validation: Validate and sanitize all input data to prevent security vulnerabilities like SQL injection or cross-site scripting (XSS).
-
Optional: Redirect to auth.php Instead of Including:
If you prefer to keep auth.php as a separate entry point, you can perform a server-side redirect instead of including the file. However, this approach is less common in a single index.php router setup.
case '/admin':
header('Location: /auth.php');
exit();
break;
Note: If you choose this method, ensure that auth.php operates independently and doesn't rely on being included within index.php.