What are some best practices for updating user online status in a PHP application?

Updating user online status in a PHP application involves regularly updating a timestamp in the database to indicate when the user was last active. This can be achieved by updating the timestamp whenever the user interacts with the application, such as loading a page or sending a message.

// Update user online status
function updateOnlineStatus($userId) {
    $currentTime = time();
    $query = "UPDATE users SET last_active = $currentTime WHERE id = $userId";
    // Execute the query to update the last active timestamp
}