What role does session handling play in restricting access to certain pages based on user age in PHP?

Session handling plays a crucial role in restricting access to certain pages based on user age in PHP by storing the user's age information in a session variable upon login. This age information can then be checked on each restricted page to determine if the user meets the age requirement to access the page.

// Start the session
session_start();

// Set the user's age upon login
$_SESSION['user_age'] = 25;

// Check user's age on restricted page
if ($_SESSION['user_age'] < 21) {
    // Redirect user to access denied page
    header("Location: access_denied.php");
    exit();
}