zhahaman2001 Posted February 26, 2007 Share Posted February 26, 2007 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 Link to comment https://forums.phpfreaks.com/topic/40232-i-need-a-good-system-to-know-if-users-are-online-or-not/ Share on other sites More sharing options...
redarrow Posted February 26, 2007 Share Posted February 26, 2007 set the time lower then like 60 sec Link to comment https://forums.phpfreaks.com/topic/40232-i-need-a-good-system-to-know-if-users-are-online-or-not/#findComment-194643 Share on other sites More sharing options...
zhahaman2001 Posted February 26, 2007 Author Share Posted February 26, 2007 but the point of this is to make it so the user cant login from two places at once... so if user "tom" logs in and waits a min then someone else can log in useing his name and there would be 2 users on the same name Link to comment https://forums.phpfreaks.com/topic/40232-i-need-a-good-system-to-know-if-users-are-online-or-not/#findComment-194660 Share on other sites More sharing options...
Orio Posted February 26, 2007 Share Posted February 26, 2007 You could run a cron job, that runs every 5 minutes or something, that sets all of the timestamps to 0 if they are more than 15 mins ago. Orio. Link to comment https://forums.phpfreaks.com/topic/40232-i-need-a-good-system-to-know-if-users-are-online-or-not/#findComment-194664 Share on other sites More sharing options...
Nhoj Posted February 26, 2007 Share Posted February 26, 2007 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... Link to comment https://forums.phpfreaks.com/topic/40232-i-need-a-good-system-to-know-if-users-are-online-or-not/#findComment-194697 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.