bcoffin Posted November 23, 2007 Share Posted November 23, 2007 Can somebody explain how these "Who's Online" features work? Do they check to see if a sessionid exists and isn't expired? Please describe the theory of how these work. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 Store a date/time that the person last logged in, query the DB and show the people that were online in the last x mins. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 23, 2007 Share Posted November 23, 2007 Just to expand on the last post, your query would look something like this SELECT col FROM users WHERE last_online > (NOW() - INTERVAL 5 MINUTE) Quote Link to comment Share on other sites More sharing options...
bcoffin Posted November 23, 2007 Author Share Posted November 23, 2007 so everytime a page is reloaded, this record is updated? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 23, 2007 Share Posted November 23, 2007 so everytime a page is reloaded, this record is updated? Yes So you would have to put the code to update their last login on your header page. Quote Link to comment Share on other sites More sharing options...
bcoffin Posted November 23, 2007 Author Share Posted November 23, 2007 just seems like a lot of db activity. I guess this is fine though. I thought maybe we could check session files on the webserver. But this, at least, I understand. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 You probably can, you just have to figure out which ones that are active and which are not. I can close my browser every 5 seconds and re-open it, and create a new session each time. Quote Link to comment Share on other sites More sharing options...
bcoffin Posted November 23, 2007 Author Share Posted November 23, 2007 so store the sessid to the user table check if an apache session file exists that matches a db record gets pretty complicated, i guess. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted November 23, 2007 Share Posted November 23, 2007 here's a script I use, that I found on the net. <?php // Grab all the currently active sessions function active_sessions() { /** * * TG WHO'S ONLINE * Copyright 2005 - 2006 (c) TOXIC GOBLIN * http://www.toxicgoblin.com * */ //Optional Database Connection Information //**Uncomment the following 2 lines and edit the values if you do not already have an active database connection** // /* $db = mysql_connect("localhost", "username", "password") or die("Could not connect"); mysql_select_db("database"); */ //Fetch Time $timestamp = time(); $timeout = $timestamp - 900; //Insert User $insert = mysql_query("INSERT INTO cf_whos_online (timestamp, ip, file) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')") or die("Error in who's online insert query!"); //Delete Users $delete = mysql_query("DELETE FROM cf_whos_online WHERE timestamp<$timeout") or die("Error in who's online delete query!"); //Fetch Users Online $result = mysql_query("SELECT DISTINCT ip FROM cf_whos_online") or die("Error in who's online result query!"); $users = mysql_num_rows($result); //Show Who's Online if($users == 1) { print("Online Now: $users"); } else { print("Online Now: $users"); } }; ?> Regards ACE Quote Link to comment Share on other sites More sharing options...
bcoffin Posted November 23, 2007 Author Share Posted November 23, 2007 That's pretty clever. So each time any page is loaded, this script (incl its 3 queries) is run. Do you find this creates heavy overhead? I imagine servers should be able to handle it, just wasn't sure whether this was considered kosh by the masses. Quote Link to comment Share on other sites More sharing options...
worldofcarp Posted November 23, 2007 Share Posted November 23, 2007 The table isn't that big, so these queries shouldn't that expensive, so a little overhead but not that big a deal. Should put an index on the timestamp column. But this is strange -- it counts users more than once if they change pages rapidly. Plus DISTINCT's may be expensive. Should use count(distinct ip) at least. Or just cut out the "file" column and use count(*) which is a million times faster. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted November 23, 2007 Share Posted November 23, 2007 Think about forums, everytime you load a new page, gobs of database information is being queried and loaded; usernames, sigs, post text, rank, avatar, everything. This little thing should hardly make your server hiccup. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.