views:

61

answers:

3

Whats the best way to keep track of how many users and guests are online? Im making a forum for fun and learning

Right Now I have a 2 fields in the users table called is_online and last_access_time.

If current time is 5 minutes or more than last_access_time i set is_online it to zero. And if the signed in user refreshes browser i set it to 1.

But what about guests? I wanna keep track on how many guests are on also

Another thing that would be really cool is to show what page the user is viewing. and on the page, forum thread for example, 5 guests, Homer and Shomer are viewing this page. But how should i structure this? Hmm maybe i should make another question for that.

i dont know what i should do

What do you suggest?

A: 

You cannot know for certain which page a user is viewing, but you can keep track of which page they last viewed. Every time you deliver a page to a user, record that page's path in a database row associated with them. Voila.

To keep the number of guests, I suggest tracking the number of distinct unauthenticated IP/HTTP-User-Agent combinations seen on a certain page in the last X minutes.

Borealid
A: 

I found this article on Web Monkey that might help you.

http://www.webmonkey.com/2010/02/how_many_users_are_on_your_site_right_now/

cfEngineers
You can also track the page that is currently being viewed by doing an AJAX request to send information in realtime to the web server to be recorded.
cfEngineers
+1  A: 

I'd use cookies for this. Set a cookie when the user enters (checking first to make sure one doesnt exist). Easy way to generate a unique id for that user is to hash their IP plus the current time.

$id = md5($_SERVER['REMOTE_ADDR'] . time());

Store that id in your database and use that to reference

You can check what page they are viewing by grabbing either $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] near the top of your php source. Store that in the table. I'd take a look at php.net's explanation of whats stored in the _SERVER global, as it should help out quite a bit if you find that you need more then just the document they are on (ex index.php). Found here.

You may need to pull apart of the query string that was used to access that page, parse out the variables to determine the page they are requesting. Either way, this could all be done through cookies, or just use a single cookie to store the unique id and use your table for storing everything else.

kmfk