iChriss Posted February 19, 2011 Share Posted February 19, 2011 Hello. I have recently been building a user system and trying to code an "Online Users" script for it however it is not working out for me, so I thought I'd come and ask here. I need a script that adds them to the table 'online' when they login and then remove them when they logout. That part is simple to do however I am using sessions and I am trying to figure out a way to check if they are inactive like every 10 minutes, and if they are delete their row from the db. It'd be appreciated if someone could set up a code for this. Quote Link to comment Share on other sites More sharing options...
trq Posted February 19, 2011 Share Posted February 19, 2011 You just described what you need to do, where exactly are you stuck? Were not here to simply pump out code for people. Quote Link to comment Share on other sites More sharing options...
iChriss Posted February 19, 2011 Author Share Posted February 19, 2011 The only way I can see this working is using time() and I've tried this and had no luck. So I just need an idea on how to go about this since all attempts I've had have failed. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted February 19, 2011 Share Posted February 19, 2011 This is simple todo, you could even do it simply from your user database, by adding a new field called 'online' set it to 'Yes' when they login and 'No' for anything else. Then retrieve all users that have 'Yes' for that field and that will give you the online users. To log someone 'out' if they become inactive, you could use a Cron-Job to update the database say every 5 minutes to check for people who not done anything in the past 10 minutes, if they haven't set 'Online' to 'No' then when they are active again, if 'Online' equals 'No' log them out and say why, makes their account safer incase they forget to logout or whatever. Hopefully this is something your looking to? Regards, PaulRyan. Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 19, 2011 Share Posted February 19, 2011 There's lots of different ways to attack this problem. Doing a custom session save handler is one. You write the session data into a mysql table rather than the default, and once you commit to doing that, you can modify the routines slightly so they do a couple of extra things: -Include the user id when the user authenticates -Add a bit of code to your custom session read() function that updates a timestamp column in the session row, you can name something like "last_seen". If you read the http://php.net/manual/en/function.session-set-save-handler.php comments there are several that include implementations you can modify slightly to do this. Once done, you have an intrinsic mechanism (the last_seen timestamp) that you can query using the mysql date/time functions to get a list of people who have actually done something on the site within the timeframe you decide indicates that they are actively logged in. 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.