Write more detailed, but I will give you a more useful thinking (a way):
First add a field in the user table in the database, and the user's last event time is like a LastActive field. Some are stored in Unix time form.
It is judged that the user online can be used if the last activity time is within two minutes of the current activity time. $ SQL = "SELECT Count (*) AS CNT from User_Table Where Lastactive 120