soycharliente Posted July 5, 2007 Share Posted July 5, 2007 I'm trying to generate a list of users that are logged in. I thought about using a lastlogin field and calculating time, but what if they stay logged in for more than that time? I thought about using a clicked field, but changing the time on that every time someone did something could get really expensive especially if there are a lot of users. I think that using session variables would be best because I can just destroy the session when they logout and it will get rid of itself if they don't do anything for a while. Can anyone give some advice as to where to go from here? Is there a way to return an array of usernames if I create a $_SESSION["user"] variable and set it when someone logs in? Or should I make some kind of associative db table with session ids and user ids? What would be the easiest way to do this and what would be a few skeleton steps to get started? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 5, 2007 Author Share Posted July 5, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 5, 2007 Share Posted July 5, 2007 What i did was added table to my database called "UsersOnline" When a user logged in, the app saved there user information, UserID, IPAddy, And The timestamp of them last being active. Now to get this to work properly you will need to add a function to the top of every page that will loop through all users that are in the UsersOnline Table.. And check their timestamps of last activity, if they havnt been active in the last 5 minutes, well then use a delete statement to remove them from the table. AFTER this function is called. I have another one that will update the users timestamp to say that they are still active. It is important that this function comes after or else they will still be logged in after more then 5 minutes. Once thats done. You just make a new page to loop through all the users that are online and display their data. I took it one step further during my last updates and when i called the function on every page for updating their information i added a new parameter to say what page they are looking at. Just an idea. Hope this helps! Andy Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 5, 2007 Share Posted July 5, 2007 i would suggest creating a SESSION var called "last_logged" with a timestamp of their last activity log. when they login, set a last_action db field to the current timestamp. also set that timestamp in their last_logged SESSION var. on each page, check if their last_logged value is greater than or equal to, for example, 5 minutes - if it is, then log their last_action to the current time in the db again, and reset their last_logged SESSION var. when you need a list of users "online," grab everyone in the table whose last_action value is 5 minutes old or less. the reason for the 5 minute check is so that it's not quite as expensive - it will only log every 5 minutes at most for each user. you can change this interval to anything (10 minutes, 45 minutes, 7 hours); the only difference will be the accuracy of your "users online" list. the smaller the interval, the more accurate, but also the more expensive. just a thought. i'm sure there are others with ideas - in fact, we've had this discussion before, but the search function on this forum isn't the greatest. EDIT: beaten to the punch, although i think my method is marginally less expensive due to its lack of DELETEing "unused" rows. Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Share Posted February 19, 2008 So, presumably, There would be a script running every 5 minutes to check that timestamp ? 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.