views:

264

answers:

2

How would one go about implementing a "who's online" feature using PHP? Of course, it would involve using timestamps, and, after looking at phpBB's session table, might involve storing latest visits in a database.

Is this an efficient method, or are there better ways of implementing this idea?

Edit: I made this community wiki accidentally, because I was still new to Stack Overflow at the time.

+4  A: 

Using a database to keep track of everyone who's logged in is pretty much the only way to do this.

What I would do is, insert a row with the user info and a timestamp into the table or when someone logs in, and update the timestamp every time there is activity with that user. And I would assume that all users who have had activity in the past 5 minutes are currently online.

Murat Ayfer
+2  A: 

Depending on the way you implement (and if you implement) sessions, you could use the same storage media to get the number of active users. For example if you use the file-based session model, simply scan the directory which contains the session files and return the number of session files. If you are using database to store session data, return the number of rows in the session table. Of course this is supposing that you are happy with the timeout value your session has (ie. if your session has a timeout of 30 minutes, you will get a list of active users in the last 30 minutes).

Cd-MaN