1👍
Using the last logged in time
doesn’t work. Your sessions can last for days/weeks so you won’t know the amount of active users that way.
One solution would be to create a table with login sessions where you store that session id in a session cookie. Session cookies expire once the user has closed his/her browser window so it should give you quite an accurate estimation of when people logged in. If you actually want to store the entire session duration, than you will also have to update the table with every page view to store the time that the user was last active. This would be slightly heavier for your database ofcourse.
Source:stackexchange.com