2

I currently maintain a DB table of users, when after logging in I update the table with their ID and login_time. This works to a point but currently I can't tell if the user has been active since the login or for how long.

Is there a better way to get a complete list of users that have been active in the past X minutes?

8
  • 5
    You could add a "last seen" column in the table and refresh that every time the user makes a new request Commented Jan 14, 2013 at 16:49
  • @Pekka That'll require another DB call so I wasn't sure that was the best solution. Commented Jan 14, 2013 at 16:50
  • @Pekka웃: What about if user is logged in but did not change the page? Commented Jan 14, 2013 at 16:52
  • 1
    Making a DB request should be okay for most sites. If you have huge traffic, you might want to switch to a memcache at some point, but a database call shouldn't cause any performance problems for a site with low to medium traffic. Any CMS-driven site without static caching makes dozens of them on every request Commented Jan 14, 2013 at 16:53
  • 1
    @Paul, The end of a "visit" can only be defined by an explicit logout or a timeout. Take a look here: stackoverflow.com/questions/679657/find-number-of-open-sessions Commented Jan 14, 2013 at 16:55

2 Answers 2

3

The best way to get what you need would be a "Last Activity" column in the users table. You would just update it whenever a user access a page. Depending on what information you need it could replace the login_time column or it could be a new column.

Sign up to request clarification or add additional context in comments.

Comments

2

You'll have to keep track of when the user made their last request in your database as a separate table or column. You can then formulate a query to select, e.g. all users that have made a request in the last 5 minutes.

PHP itself does not store - or care for - that kind of information. Unless you happen to have your own session management module which does store this kind of information, then you could use data from that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.