Jump to content

Page Views


jbingman

Recommended Posts

Hi,

I'm trying to get a code for showing the current number of people viewing the page. It seems like something that would be PHP OOP but I cant find anywhere online to help me.

it would just say something like , "[blank] number of guests currently viewing this page" something along those lines.

 

If its not PHP please move this to somewhere I can get this info. THANKS!

Link to comment
Share on other sites

it's not really anything specifically OOP - it can be done in a number of ways.

 

The way I do it is to use custom sessions - ie, storing the sessions in a database so that sticking stuff in $_SESSION will put it into the database instead of into files. This way I can query who is online, where they are, etc. SMF I believe does this, as does phpBB etc. Take a look at session_set_save_handler() for more info to see if this is what you want.

 

Simpler ways would just be to log a URL and a timestamp each time someone hits a page, then just querying who has been on a certain page within a certain time. 2-5 minutes is realistic enough as there's no real nice way of knowing whether someone is still actually viewing the page, just idle, or closed their browser tab.

Link to comment
Share on other sites

depending on what you want will determine what you need to store. if you just want a relatively simple "how many online", then on each page hit, store the user's ID (if applicable, else store zero to signify a guest), IP address and the timestamp in the database. after that, just an SQL "SELECT" to get the number of unique IP's from the table created since a particular time (ie, within 5 mins). problem - if a group of users from, say, a company, log in - ie, they all share the same IP address - then you might cause confusion.

 

best way (though still not 100% accurate) is to use custom sessions as I mentioned in my last post. Regardless of IP, the session id will be unique to each user. So get your custom session handler to store the session id, user id and timestamp (along with the session data - read the notes within the manual for session_set_save_handler) when the user hits the page and do the query as with the other method. like i say, this is the most common way of doing it as used by many forums and CMS's, so is probably your best and most reliable bet.

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.