Jump to content

Making a "Who is online" System


N-Bomb(Nerd)

Recommended Posts

Hello,

 

I'm trying to create a system where I can monitor where people are at on my website (so I'll be able to see which specific page their on). However, I've ran into a snag, because I have no idea where to start. There isn't an account system used so it has to be based off ip address and I only wanted to display active people within the last 15 minutes. So, I'm assuming this has to be session/cookie based, but I've never had to work with that before. I'm a complete loss and I've tried looking up tutorials online, but I'm not getting any real good results.

 

Could anyone point me in the right direction?

 

Thanks.

Link to comment
Share on other sites

1. Create a table to store the activity in. You'll need columns for at least 'ip' and 'page'. NOTE: You can have many users with the same IP if they are behind a router using NAT. So, you can't be sure that each IP is a unique user or not.

 

Anyway, on each page load you will want to insert and/or update records in the table with the ip of the user and the page they requested. So, you could also use a session or cookie value to try and identify separate users on the same IP. Each has some benefits and drawbacks. Cookies can be deleted by the user or the user might not even accept cookies. But, on the other hand, cookies can be set to persist over time so they will be available over different sessions allowing you to track users consistently. Sessions cannot be modified by the user, but they also expire when the browser is closed. So a user who access your site, closes the browser and re-accesses the site will show two different users.

 

If think you will ONLY ever want the current page of the user, then you would use an "INSERT ON DUPLICATE KEY UPDATE" query. That way if there is already an entry for that IP the previous entry will be overwritten by the new one.

 

However, if you think that the historical data will ever be needed, then always do an insert. You can get the users most recent activity by applying the correct ORDERing and GROUPing logic in the query.

Link to comment
Share on other sites

1. Create a table to store the activity in. You'll need columns for at least 'ip' and 'page'. NOTE: You can have many users with the same IP if they are behind a router using NAT. So, you can't be sure that each IP is a unique user or not.

 

Anyway, on each page load you will want to insert and/or update records in the table with the ip of the user and the page they requested. So, you could also use a session or cookie value to try and identify separate users on the same IP. Each has some benefits and drawbacks. Cookies can be deleted by the user or the user might not even accept cookies. But, on the other hand, cookies can be set to persist over time so they will be available over different sessions allowing you to track users consistently. Sessions cannot be modified by the user, but they also expire when the browser is closed. So a user who access your site, closes the browser and re-accesses the site will show two different users.

 

If think you will ONLY ever want the current page of the user, then you would use an "INSERT ON DUPLICATE KEY UPDATE" query. That way if there is already an entry for that IP the previous entry will be overwritten by the new one.

 

However, if you think that the historical data will ever be needed, then always do an insert. You can get the users most recent activity by applying the correct ORDERing and GROUPing logic in the query.

 

Thank you for the reply. I'll be diving into this later today so if anyone has any other input that would be appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.