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?
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.
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.