How many users online

hi all

i was trying to show “How many users are logged in now”

So my question is :

  1. What kind of timestamp do you insert in database for user login ??
hours and minutes
hours and minutes and seconds
  1. Do you show the user as logged in till he hasnt logged out or
    you show user logged in according to timestamp.

Means if a user logged-in at 8:40 and if i refresh the page at 8:41

will he be shown as logged in now or you will again count users at 8:41 ??

  1. Do you record status of user in the database as “logged-in” or “logged-out” ??

vineet

I’d say it depends on what kind of site it is, but also that a lot of users don’t bother logging out of a lot of sites - probably more likely to log out of a banking site than a forum site, for example. So you probably need to decide on a time-out period and once they haven’t been active for that time, count them as logged out. That time-out period would go towards answering the first question, though I would think only a few sites would need to care about seconds for a login time.

hi droopsnoot

i can check if the user is logged-in or not.

But how to check if user is inactive for 10 minutes or 15 minutes ??

vineet

Is the timestamp being recorded in the users table (or in the sessions table if you’re storing sessions in the database)?

timestamp is not getting recorded.

But i can create a timestamp field in database if you say

vineet

I suggest storing the timestamp (like PHP’s timestamp()) in the database.

Then as the user is browsing, check that the timestamp is not older than say… 60secs/120sec/5mins and then update it. This would depend on how you’re retrieving your user data because if you’re not getting it on every page load, it may be hard to check.

You could use a heartbeat script where JavaScript in the web page calls a server side script at specified intervals to tell the server the page is still open.

For example http://javascriptexample.net/ajax09.php

If the user has “SAVE LOGIN PASSWORD” option turned on in the firefox or chrome

then will i be able to auto login him if he is not active ??

will the auto logout code work if “save login password” option is turned on ??

vineet

that generally works by saving a cookie on their computer that contains the information needed for them to be able to login automatically when they next visit.

someone being actually logged in requires that a session exist to track that they are logged in from one page to the next. If they close their browser without logging out then they will lose access to the session from their end and so would need the autologin cookie to log them back in with a new session on their next visit.

Also the server usually cleans up sessions that have not been properly unset (by logging out) after a specified period of time (eg. two hours) so someone actually logged in and active for that amount of time would find themselves automatically logged out by the cleanup process.

I do not recommend setting up to save the password on the client end as it makes it much easier for someone else to break into that account.

My solution is to work with mysql and events.

Create a table user_online.

Makesure there is one INT entry called online.

If a user logges in add his credentials to this table.
This is the initial state.

examp.

   $qry =   "CREATE TABLE `user_online` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `userid` INT NULL,
            `session` VARCHAR(50) ,
            `ipnr` VARCHAR(20) ,
            `online` int(11) default 0,
            `creationdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
             PRIMARY KEY (`id`)
            )  ENGINE=InnoDB DEFAULT CHARSET=utf8 OLLATE=utf8_unicode_ci ";

Add an event in mysql which will decrease the online value with 10 every 10 seconds. Or whatever time you want to use.

examp.

create event online_event2 on schedule every 10 second do
update dbase.user_online set online= time_to_sec( timediff( user_online.creationdate,DATE_ADD(NOW(),INTERVAL 0 SECOND ) ) )

Create a second event which checkes the online value every 10 or 20
seconds and if it exceeds the max life time delete the entry from the
user_online table

create event if not exists online_end_event on schedule every 20
second do delete from dbase.user_online where TIME_TO_SEC( TIMEDIFF(
user_online.timestamp, DATE_ADD(NOW(),INTERVAL 0 SECOND)
) ) < -1440.

Were 1440 is the max lifetime in secs.

Query the user_online table to see how many are online
And every user action set the online field to 0 in the user_online table.

If a user closes the browser or does log out or your machine crashes.
Mysql will continue with the trigger and always get to the point were the user is not online anymore.

Hope it helps.

Peter Lemmen

I have a field in my sessions table for the id of a user (all users that aren’t logged in have a user id of 0 and are treated as guest users). To get a list of online users I do a left outer join query with the users table. Any user with a last online within around 2-5 min of the current time I’d consider “online”, between 5-10 mins of the current time to be “idle” and anything longer then that to be “offline”

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.