PC Nerd Posted April 3, 2007 Share Posted April 3, 2007 hi guys ive seen that part in the header of this forum when your logged in, and it tells you how much time youve spent online and active..... im wondering how to do this. im using cookies, with an expire time of 30 minutes, BUT people might just not loggout, and the timout is to check that how can i loggout people...., or is there a better way of running the loggin i know its very open question, but im just looking for a starting point and pointer. thanks guys Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 3, 2007 Share Posted April 3, 2007 Have a table storing the sessions. Then each time a user go to a new page you update a timestamp. In that way you can check if they have been active in the last X minutes. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 3, 2007 Author Share Posted April 3, 2007 coudl you give me a rough example, i think i inderstand, but am veru confused.... thanks for the answer though.... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 3, 2007 Share Posted April 3, 2007 Ok. You have a table like this: user_sessions ============= sid <-- unique id s_last_activity <-- integer holding a UNIX timestamp s_user <-- could be the user id or username Now each time a user goes to a new page on your site you could run this: mysql_query("UPDATE user_sessions SET s_last_activity='".time()."' WHERE s_user='{$user_id}' LIMIT 1"); Then to check if a user has been active in the last 15 minutes you'd do this: <?php $result = mysql_query("SELECT * user_sessions WHERE s_user='{$user_id}' LIMIT 1"); if(mysql_num_rows($result) <= 0) { echo "No session exists. Not logged in"; } else { $session_info = mysql_fetch_assoc($result); if($session_info['s_last_activity'] >= strtotime("-15 minutes")) { echo "Has been active within the last 15 minutes. Is logged in."; } else { echo "Has not been active within the last 15 minutes. Not logged in."; } } ?> If the user logs out then delete its session in the database. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 3, 2007 Author Share Posted April 3, 2007 ok, thanks ill look into it, do you have any sites of resources i should look at to get started? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 3, 2007 Share Posted April 3, 2007 No, but you could try looking at the source of already built systems to see how they do. Such as SMF which is being used here. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 3, 2007 Author Share Posted April 3, 2007 lol, i downloaded SMF and looked at the code, and couldnt read the firts statement. its not designed well at all, in terms of readability..... all the code is simply in 3 or 4 lines, for the main mpage alone......... so i couldnt read it ive also looked at phpbb, but its the same ive also looked at joomla, and am looking at moodle if it has that function... anything elsed i should look at 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.