Jump to content

Check if users are online


scrubbicus

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.