How can PHP sessions be effectively managed and passed between pages when using dynamic includes for different sections of a website?

When using dynamic includes for different sections of a website, it can be challenging to manage PHP sessions effectively and pass them between pages. One way to solve this issue is by starting the session on every page that includes the session data and ensuring that the session is properly initialized before any session data is accessed or modified.

// Start the session on every page that includes the session data
session_start();

// Check if the session is already started before starting it
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Ensure the session is properly initialized before accessing or modifying session data
if (!isset($_SESSION['user_id'])) {
    $_SESSION['user_id'] = 1; // Example of setting a session variable
}