How can PHP be used to display who is currently logged in to a website and how many guests are present?

To display who is currently logged in to a website and how many guests are present, you can store user login information in a database and track guest sessions using session variables. You can then query the database to retrieve the list of logged-in users and count the number of active guest sessions to display this information on the website.

// Assuming you have a database connection established

// Query to retrieve list of logged-in users
$logged_in_users_query = "SELECT username FROM users WHERE is_logged_in = 1";
$logged_in_users_result = mysqli_query($conn, $logged_in_users_query);

echo "Logged in users: ";
while($row = mysqli_fetch_assoc($logged_in_users_result)) {
    echo $row['username'] . ", ";
}

// Count active guest sessions
$active_guests = count($_SESSION['guests']);

echo "Guests online: " . $active_guests;