<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Check if the user is logged in.
// If not, redirect them to the login page.
if (!isset($_SESSION['user_id'])) {
    // Store the requested URL in session to redirect after login (optional)
    // $_SESSION['redirect_url'] = $_SERVER['REQUEST_URI'];

    header("Location: login.php");
    exit();
}

// Optional: You might want to re-fetch user data here if it can change
// and you need the freshest data on every authenticated page load.
// However, for basic auth check, just checking 'user_id' is usually sufficient.
?>
