Ashoar Posted March 29, 2010 Share Posted March 29, 2010 Had a search of the forum but couldn't really find much that related to much to my desired result. Currently when a user logs in, once the system has authenticated them it will then log their details into an "online" database table like so: if($row['Activated'] > 0) { $_SESSION['s_logged_n'] = 'true'; $_SESSION['s_username'] = $username; $_SESSION['s_name'] = $row['Name']; $insertuser="INSERT INTO online(Username) values('$username')"; mysql_query($insertuser) or die("Could not login insert user"); header("Location: index.php"); } else { Then in my information center i just pull all results from the "online" table to display the users online. Now off course by using this method the only possible way a name can be removed from the database is if the user manually hits the logout button where i then run: $username = $_SESSION['s_username']; $query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['s_username'])."';"; $result = mysql_query($query); $_SESSION['s_logged_n'] = ''; $_SESSION['s_name'] = ''; $_SESSION['s_username'] = ''; session_destroy(); If a user just closes the browser without logging out they will stay logged in for the default php session length before the session is destroyed. The problem here is that it won't remove them from the online table meaning they will always be displayed in users online. When they login next their name will then appear 2 times in users online and only get taken off if they actually click logout. Now i can always add: ini_set(’session.gc_maxlifetime’, ‘0′); Which would mean the session would not end unless they click log out regardless of whether they leave or not. Now i am absolutely hopeless with sessions with timestamps, so does anyone have some insight as to how i could implement it into the current code and the code of each page for the timestamps? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 29, 2010 Share Posted March 29, 2010 When they login next their name will then appear 2 timesYou should make the Username column a unique key to prevent that. You need to store the 'last access' date/time in the online table as well. You would UPDATE the last access date/time on every page request to keep it current. You can then check and remove records in the online table that have a last access date/time older than a value you pick (typically 10-20 minutes is used.) Quote Link to comment Share on other sites More sharing options...
Ashoar Posted March 29, 2010 Author Share Posted March 29, 2010 Thanks for the reply. That is the part i know, i'm just stumped as to how i can apply it or add it to the current code i have there. As i said above i am dreadful when it comes to sessions considering their simplicity. Any chance of showing how it can be implemented there? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 29, 2010 Share Posted March 29, 2010 It has nothing to do with sessions and in fact I did not even mention the word session in my reply. There are countless 'who is online' php scripts posted around that show how to add/check/remove records from an 'online' table - http://www.google.com/#hl=en&source=hp&q=php+who+is+online+script&aq=0&aqi=g1&aql=&oq=php+who+is+online&gs_rfai=&fp=bcdf8cbbf06dc4f Quote Link to comment Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 When they login next their name will then appear 2 timesYou should make the Username column a unique key to prevent that. You need to store the 'last access' date/time in the online table as well. You would UPDATE the last access date/time on every page request to keep it current. You can then check and remove records in the online table that have a last access date/time older than a value you pick (typically 10-20 minutes is used.) You can apply this method to your users table aswell just add a last_access field and update on each request. UPDATE users SET last_access = now() WHERE id = $uid To select all 'logged in' users: SELECT username FROM users WHERE unix_timestamp(last_access) + 300 > now() A user is allowed 5 minutes to be idle before he is considered logged out Quote Link to comment Share on other sites More sharing options...
Ashoar Posted March 29, 2010 Author Share Posted March 29, 2010 I am aware of the numerous ones online for general visitor count but my original question was more so aimed to implementing one within my current script which is the part i was having trouble with. Quote Link to comment Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 I'm just saying that having the same information (username) scattered across the database is a bad idea much more so when a simpler solution is at hand Quote Link to comment Share on other sites More sharing options...
Ashoar Posted March 30, 2010 Author Share Posted March 30, 2010 I will be looking in to fixing that Ignace, just looking at getting this going first. Any further suggestions at setting this up? Quote Link to comment Share on other sites More sharing options...
Ashoar Posted March 30, 2010 Author Share Posted March 30, 2010 Still having trouble working this out 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.