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