How can PHP sessions be properly utilized to manage user authentication and authorization?

To properly utilize PHP sessions for user authentication and authorization, you can store user credentials in the session upon successful login and check for these credentials on protected pages to verify the user's identity.

// Start the session
session_start();

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