What are the best practices for organizing PHP code to ensure that all pages, including the login form, are accessed through the index.php file?

To ensure that all pages, including the login form, are accessed through the index.php file, you can use a routing system in your PHP application. This involves directing all requests to the index.php file and then using the requested URL to determine which page to display. By organizing your code in this way, you can centralize the logic for handling different pages and ensure a consistent structure for your application.

// index.php

// Define a list of valid pages in your application
$validPages = ['home', 'about', 'contact', 'login'];

// Get the requested page from the URL
$page = isset($_GET['page']) ? $_GET['page'] : 'home';

// Check if the requested page is valid
if (in_array($page, $validPages)) {
    // Include the corresponding page file
    include $page . '.php';
} else {
    // Handle invalid page requests
    echo '404 Page Not Found';
}