Jump to content

I need a good system to know if users are online or not


zhahaman2001

Recommended Posts

Ok I have read up on it and found a few ways to do this most of which wont work for me... the way that would mostly work is when a user logs in it changes a timestamp to the current time in the database under his/her table and if they log out it changes it to 0 (sessions time out in 15 minutes) so to find out if user is logged in is a simple if statement

ex if timestamp > currenttimestamp – 15 (that’s not really it but you get the idea of how it works) the problem with this system is if the user closes the browser with out logging out and opens it back up they no longer have a session and they are logged out but the database will read that they are still logged in for the next 15 minutes because it doesn’t know the session has ended

So is there a workaround so I can use this system or better yet is there a better system i can use? Thanks in advance for the help

 

What I do on more than one site is have one table called like, `sessions` and in it have something like...

 

CREATE TABLE `sessions` (
  `uID` smallint(5) unsigned NOT NULL default '0',
  `uTime` int(10) unsigned NOT NULL default '0',
  `uLastOnline` int(10) unsigned NOT NULL default '0',
  `uIP` varchar(16) NOT NULL default '',
  `uCode` char(32) NOT NULL,
  PRIMARY KEY  (`uID`),
  KEY `uTime` (`uTime`,`uLastOnline`,`uIP`,`uCode`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

Now, when someone logs into the site (I use sessions), the script generates a new UNIQUE `uCode` # using

mt_rand()

. Once it's got a unique new number it updates the DB and then adds the session # to a session variable for the user. At the top of each page I have it set to update the users `uTime` by doing something like...

 

mysql_query('UPDATE `sessions` SET `uTime` = '.$_SERVER['REQUEST_TIME'].' WHERE `uID` = '.$user['uID']);

 

If you use php 4 you'd need to use

time()

instead of

$_SERVER['REQUEST_TIME']

 

In order to check if a user is even online at the very very top of every page it runs a simple query like...

 

$user = mysql_fetch_assoc(mysql_query('SELECT `uID` FROM `sessions` WHERE `uCode` = '.$_SESSION['uCode']));
if (!$user) {
// THEY ARE NOT LOGGED IN
} else {
  // THEY ARE LOGGED IN
}

 

Now, if you want to count the users online you do something like...

$usersonline = mysql_result(mysql_query('SELECT count(0) FROM `sessions` WHERE `uTime` > '.($_SERVER['REQUEST_TIME'] - 60)), 0, 0);

 

Now basically the way this works in lamens terms is...

a) user logs in and gets a unique #, that # is stored in their session and in the DB as their ucode.

b) if someone else logged in as them the old user would be kicked out because his session # doesn't match the DB # anymore

c) the utime is stored in a unix_timestamp form and basically all you do is select the # of users who have had it updated in the last 60 seconds.

 

 

I've used this method on a lot of sites I create for other people and so far it's yet to fail me...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.