4

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.

Velome
  • 158
  • 1
  • 1
  • 15

5 Answers5

7

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
  • 3,894
  • 6
  • 29
  • 26
  • "And update the timestamp every time there is activity with that user" , So, a each time a user opens a page, we update the table? – Sobiaholic Aug 16 '13 at 00:10
4

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).

Grey Panther
  • 12,870
  • 6
  • 46
  • 64
0

i think you can make it simply in php

create table user consist of , username , password and status(1,0)

  1. in login system analyse username and password
  2. then if username and password is match , run the query to select from table user , and fetch it
  3. then get the status
  4. Update user , set status into 1 , it means online
  5. When you click logout , analyse session username and password , and select query from tbl user and got the status , and then set status into 0 it means offline
mcols27
  • 11
  • 2
0

I was thinking to do it this way:

When the user logs in, his userid and timestamp will be inserted in to a table. Then for every 5 minutes, i will call a php script via ajax to check whether the user is logged in, and if so, update his timestamp in the table.

If the user is not logged in, just delete his record.

shasi kanth
  • 6,987
  • 24
  • 106
  • 158
0

There are many ways of implementing this.

You can implement this simply by polling.

Check clients by certain interval and count the numbers of clients replied back.

That value can be used as number of online users.

I think better way is using push technology instead of checking online people certain every x seconds or x minutes.

It simply let server know when people are entering and leaving by event.

So then server just increase and decrease the the online user counting variable when events come from clients.

I recommend Socket.IO, APE to look at.

Also there are many other to consider such as XMPP, Jabber.

jwchang
  • 10,584
  • 15
  • 58
  • 89