Jump to content

ive php ?


EchoFool

Recommended Posts

Is it possible with php to use it in a live sense without refresh so that I can create an accurate "total users viewing this page" kind of thing.. so as soon as they

 

A) close the window

B) change the page on the site

 

It updates the counter in the php ?

 

If this is beyond PHP control what language can do this kind of live checking ?

Link to comment
Share on other sites

But you want to do it when the page closes. You might be able to do something on the

 

That would be an option but, just note that it won't be 100% accurate.  Users can disable JS and I don't think the unload() function is supported by all browsers.

Link to comment
Share on other sites

would be more effective to use ajax to just like, ping the server every 10-30 seconds, depending on how much error of margin you're willing to live with.  Less margin == more resources being uses.  But that still falls under category of users being able to disable js. Can't get around that.  I suppose you could get creative and have a dynamically created transparent 1x1px image called from image.php with an id attached to it (image.php?id=xxx) in an iframe that's meta refreshed every x seconds.

Link to comment
Share on other sites

Also how would the 1x1 image be counted for exactly?

 

The 1x1 image is done as a request to a php page, passing an id via get method. For example:

 

<img src='somepage.php?id=12345' />

 

somepage.php would then grab the id from $_GET['id'] to see that the user associated with the id is still there.  You could use various info from the $_SERVER array that gets populated from the request, to track other stuff about the user, or you could add more vars to the query string.  For instance, maybe a report-friendly page name:

 

<img src='somepage.php?id=12345&p=pagename' />

 

So your image source would point to a script, vars passed, it processes the vars, and you would use the gd library to just load up or create on the fly just a simple little 1x1 transparent image and output it.  Have that in an iframe with a meta refresh of every x seconds, and you've got yourself a non-javascript pinger.

Link to comment
Share on other sites

You don't really need an image.  Basically, this technique is generally described as ajax polling.  You need an xmlhttprequest routine, and on the php side a counter script.

 

In javascript you can set a timer using the setTimeout() function.

 

setTimeout() takes the name of a function to call as it's first param, so if you create a function that does something, and then you call setTimeout() in that function, and specify the function name, you get a timer that will continue as long as that page is open.  As CV stated, the only question is how often should this function run?  Every time it does run it's going to execute this script on your server, so if you have a lot of users, this could put a lot of load on it. 

 

 

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.