Can I count sessions to determine number of people online?
Solution 1
Totally wrong logic. $_SESSION is a per-user thing. One user's session is not shared with any other user's session. Think about it - an online bank written in PHP, all sharing a single $_SESSION - everyone would see everyone's account details.
Assuming you're on the standard PHP file-based sessions, you can count the session files in whatever directory they're stored, e.g.
$users = count(glob(session_save_path() . '/*'));
Note that this just counts session files - it will undoubtedly contain stale/dead sessions that haven't been garbage collected yet. If you want an actual "really is online right now", you'd have to parse each session file and examin its contents.
Solution 2
At first, you have to define what "to be online" means. Should the user have clicked on a link within the last 5 minutes?
I assume that you already have a user table in your database.
So the simplest way is to add a new column, e.g. lastAction TIMESTAMP
.
And when the user clicks on a link on your page, your script should update this value.
And on your statistics page or whatever, you get the number of online users with that code:
SELECT COUNT(*) FROM users WHERE lastAction > (NOW() - 60*5)
Solution 3
PHP is pretty flexible in terms of session storage, you can define your own session save/restore handlers.
However, the default session storage is files
, where each session is individually stored to disk. Which means, that in order to find out how many users there are "online" (here, i assume "online = session exists with $_SESSION['$nickname']
set"), you would need to open all the session files stored on disk, and check how many unique nicknames exist within them. This is very heavy in both time and required resources.
Hence, most tutorials suggest counting this in the database, by maintaining a last-seen timestamp per user (and checking how many users were last seen in last X minutes).
If you wish to combine, that is doable via defining your own session save handler to store session information in the database...
Several additional notes on sessions:
- Sessions are not destroyed immediately when closing a browser. In fact, the browser does not tell the server in any way that it is being closed. It means that the server should come up with some time-based algorithm to decide who's online and who is not.
- Sessions are isolated from one another so
$_SESSION["foo"]
can not be shared by multiple different sessions - there is no equivalent toGlobal.asa
in PHP. Not off the box.
Slavez
Updated on June 14, 2022Comments
-
Slavez almost 2 years
I am confused about something. When I try to search how to count online users in PHP, all answers related with MySQL and many different ways.
In my script, any user that submits the login form creates a $_SESSION['$nickname']
So I thought, can I count login sessions with count($_SESSION['$nickname']); and show it in my page?
Or is this totally a wrong logic?
-
Slavez over 11 yearsYes, this is good information but it will create too many MySQL connections and updates. I just want count users that created a session. We know if they shut down their browser or close my site, session will be destroyed.
-
ComFreek over 11 yearsIf you only want to count the current number of session (files), Marc B has already posted your solution.
-
Slavez over 11 yearsThis was very helpful and I am going to do some changes with this information, thank you.
-
Slavez over 11 yearsI really loved lastaction way of counting online users, by the way. Does it tire the server? Cause, every page will make an update, you know.
-
poncha over 11 yearsThere are two problems here: (1) not every session might have a
nickname
in it, and (2) those nicknames are not necessarily unique... -
poncha over 11 yearsWhy
INTEGER
btw? you compare it to time, so why notTIMESTAMP
? -
Slavez over 11 years"This is the wrong logic and the session is only stored locally on the end user's PC" this is true information for Cookies not for Sessions.
-
poncha over 11 yearsoh, and about garbage-collected files... you can use
find
(execute) to find how many session files were modified in last N minutes... -
ComFreek over 11 years@Slavez That depends on your server and on the number of users you have. But it isn't as expensive as you think.
-
Delorean over 11 yearsTrue, but I guess I should clarify that sessions are destroyed (locally) immediately when a browser is closed, but not on the server. The server can retain it for much longer.
-
Cups over 11 yearsYou might investigate how to store sessions for this app in a different directory, also set a shorter life for them (but be careful -- not too short) then do the equivalent of "-latr" and count just those created in the last x minutes.
-
Sablefoste over 6 years@Slavez, I don't think you can determine if a user shut down their browser; consider if the user closes a tab, and the "reopens the last tab" with a
ctrl-shift-t
. If the $_SESSION hasn't expired, it should reopen just find. Unless you are constantly pinging the server and have a very short expiration time for the $_SESSION.