scrubbicus Posted May 29, 2009 Share Posted May 29, 2009 I'm trying to set something up on my site to show all users currently online. I tried looping through my database and checking if $_SESSION['username'] == $row['username'] then echo 'Online' but only works for me being online. So I'm thinking of adding a column to the users table for online/offline and whenever someone logs on it updates that too online then logs off it updates it too offline. Is this the best way or is there another way? Thanks. Quote Link to comment Share on other sites More sharing options...
Alt_F4 Posted May 29, 2009 Share Posted May 29, 2009 this is what i would do: adding a column to the users table for online/offline and whenever someone logs on it updates that too online then logs off it updates it too offline i'm not saying that there may not be a better solution, but that is what I would do. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 29, 2009 Share Posted May 29, 2009 Yeah, the reason that your session code doesn't work is that a session is relative to a single browser instance. There's a number of different approaches to this, each having its own issues. One method would be to use a custom session handler that stores the session information in a database. You would need to add an extra column to store the username in the session table and add some extra code. There are examples around you can find of how to accomplish this, some of which are linked off the php.net manual page for session handlers. I would not add an online flag, because usually people don't "logout" of a server, they simply navigate away or close their browser. Even sessions have this issue, although there's a built-in mechanism that cleans up expired sessions. If you do want to utilize a database flag, then I'd suggest you make it a DATETIME column named "lastlogin" or something similar. What you then code, is something that shows you all the people who have a lastlogin DATE within a certain number of minutes This works well, and is simple, however, the problem you have is that you need the date to be updated when users navigate to other pages, so you need some code to handle that. All those updates can put quite a lot of strain on the database, and can cause locking issues. An improvement over the general idea, is to have a table that does nothing but store "page views" with the time -- so for example, you might have only the username and a timestamp. When a user who is logged in, visits a page, you create a routine that inserts a row into this table. Again, you can query it within a time range (last 15 minutes for example) and doing a GROUP BY on the username, you have your list of logged in users, without the concern that you'll be creating too many update locks on your user table. This method works very well and doesn't degrade in performance even if you don't delete old rows from it, so long as you have the right indexes on the datetime and username columns. I'd recommend this solution over the others. Quote Link to comment Share on other sites More sharing options...
Alt_F4 Posted May 29, 2009 Share Posted May 29, 2009 Ha ha, see a better solution! 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.