How can one check if a specific user is logged in using PHP sessions?

To check if a specific user is logged in using PHP sessions, you can store a unique identifier for the user in the session when they log in. Then, you can check if this identifier exists in the session to determine if the user is logged in.

session_start();

// Check if the user is logged in by checking for a specific session variable
if(isset($_SESSION['user_id']) && $_SESSION['user_id'] == $specific_user_id) {
    echo 'User is logged in.';
} else {
    echo 'User is not logged in.';
}