How can a PHP page automatically check if a user is logged in and display the appropriate menu?

To automatically check if a user is logged in and display the appropriate menu in a PHP page, you can use session variables to track the user's login status. When a user logs in, set a session variable to indicate that they are logged in. Then, on each page where you want to display the menu, check if the session variable is set to determine if the user is logged in and display the appropriate menu accordingly.

<?php
session_start();

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
    // Display menu for logged in users
    echo '<a href="profile.php">Profile</a> | <a href="logout.php">Logout</a>';
} else {
    // Display menu for non-logged in users
    echo '<a href="login.php">Login</a> | <a href="register.php">Register</a>';
}
?>