Andrew777 Posted July 25, 2011 Share Posted July 25, 2011 Hi, I'm creating a membership section where once a member logs in, an entry is created in the "onlinenow" table. And I use that info to display who is online in a small box on the home page. When they physically logout, the logout page erases that entry in the "onlinenow" table. BUT if the user isn't active for a while the session expires but the entry in the table obviously stays intact, so it looks like they are still online. Can someone tell me how to fix this issue? Or if there is a better way to show who's online now instead of a separate table that logs logins? I've searched google for an answer but the discussions about sessions seem kind of over my head at this point... Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/242688-session-logout-time/ Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 You could store their session expiration time in the onlinenow column instead. Then you can reference it where now < sessionExpire Quote Link to comment https://forums.phpfreaks.com/topic/242688-session-logout-time/#findComment-1246467 Share on other sites More sharing options...
Andrew777 Posted July 25, 2011 Author Share Posted July 25, 2011 Thanks for the help teynon, I'm not sure how to figure out their session expiration time though? If a person logs in, how do I know how long before the session expires after inactivity? Quote Link to comment https://forums.phpfreaks.com/topic/242688-session-logout-time/#findComment-1246511 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 If the sessions aren't time based (they end when the user closes the browser), you'll have to make up a time. Usually something like now+5 minutes. Basically the logic is: User does action. User marked active at this time. Find all users active within the last X minutes. (These users are considered online.) You could add a buffer zone as well. Users within 5 minutes = online. Users within 5 - 10 minutes = idle. Quote Link to comment https://forums.phpfreaks.com/topic/242688-session-logout-time/#findComment-1246513 Share on other sites More sharing options...
Andrew777 Posted July 25, 2011 Author Share Posted July 25, 2011 i guess mine are automatically time based because if i leave the browser open long enough, the session will expire, but I still don't get how to set a certain amount of time and how to cause a check and then get rid of that onlinenow entry in the table after that? :-( Quote Link to comment https://forums.phpfreaks.com/topic/242688-session-logout-time/#findComment-1246517 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Here is what I would do: <?php session_start(); // Do all user login validation Blah blah blah validation code. // User is now active. Update user time. $timeout=date("YmdHis", strtotime("+5 minutes")); $sql="UPDATE table SET onlinenow = '{$timeout}' WHERE ID = userID"; @mysql_query($sql); $now=date("YmdHis"); // Get all online users: $sql="SELECT * FROM table WHERE onlinenow > '{$now}'"; -- Loop through all online users here and do what you want with them. ?> Quote Link to comment https://forums.phpfreaks.com/topic/242688-session-logout-time/#findComment-1246519 Share on other sites More sharing options...
Andrew777 Posted July 25, 2011 Author Share Posted July 25, 2011 Thank you Teynon, I appreciate it. I will go through this and see if I can get working on my site. Thanks again!!! Quote Link to comment https://forums.phpfreaks.com/topic/242688-session-logout-time/#findComment-1246521 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.