I'm trying to find a way to have a page "listen" to which users are live. For example, let's say two users are about to go on the same page. When the first user gets to the page, his name will show up. Then when the next user gets to the page, he'll see the first user's name with his name below it. And when one leaves, their name disappears. How would I go about this? I am currently using PHP and JavaScript on my website.
-
You will first have to store the IP addresses of every user in a database or a temporary file. – Blender Nov 15 '11 at 04:54
-
IP addresses are **not** unique user ids there is thing thing called NAT! – Nov 15 '11 at 04:57
2 Answers
Jarrod is correct, the IP alone is not sufficient. I would also like to point out that JavaScript alone will not be enough to do this either. The best thing I could come up with would be to have PHP return a list of all of authenticated and active users based off the session. You could then set up a script on the client side to poll the back-end script you write and just refresh some section of the page with the list provided. For specifics regarding PHP user sessions please see: Looping Through All a Server's Sessions in PHP
You can't do what you want unless you can detect when they leave. Even with backend support you will not know they left unless you can capture the unload event ant get it across to the server. Then you still will not know when they re-enter using the back button. If you do come up with some kind of continuous polling that is reliable (not likely), then the server and bandwidth load would be substantial.
The best you should be shooting for is a list of recent visitors.

- 284
- 2
- 6