How can PHP sessions be effectively used to manage user login status and access control?

To manage user login status and access control using PHP sessions, you can store the user's login status and access level in session variables. When a user logs in, set a session variable to indicate they are logged in, and possibly store their user ID or access level. Then, on restricted pages, check if the session variable is set to allow access.

// Start the session
session_start();

// Check if user is logged in
if(isset($_SESSION['logged_in'])){
    // User is logged in, allow access
} else {
    // Redirect to login page
    header("Location: login.php");
    exit();
}