How can sessions be effectively used in PHP to manage user authentication and access control?

Sessions can be effectively used in PHP to manage user authentication and access control by storing user information in session variables upon successful login and checking these variables on each page to determine if the user is authenticated. Access control can be implemented by setting different session variables based on the user's role or permissions.

// Start the session
session_start();

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