EchoFool Posted April 18, 2009 Share Posted April 18, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/ Share on other sites More sharing options...
jackpf Posted April 18, 2009 Share Posted April 18, 2009 Sounds liek a job for ajax to me. (A combination of javascript and php) Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-812878 Share on other sites More sharing options...
EchoFool Posted April 18, 2009 Author Share Posted April 18, 2009 So the ajax is live and calls the php functions ? Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-812879 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 ajax is a javascript method where you use some js functions to make a request to a server-side script and receive results, whenever events on the page occurs, like element clicks, focus, blur, etc... Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-812904 Share on other sites More sharing options...
keeB Posted April 18, 2009 Share Posted April 18, 2009 But you want to do it when the page closes. You might be able to do something on the <body unload attribute, but I am not entirely sure. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-812906 Share on other sites More sharing options...
Maq Posted April 18, 2009 Share Posted April 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-812914 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-812919 Share on other sites More sharing options...
keeB Posted April 18, 2009 Share Posted April 18, 2009 Only thing there is tracking sessions. If session has been idle for > $threshold, increment counter by 1 and close session. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-812923 Share on other sites More sharing options...
EchoFool Posted April 18, 2009 Author Share Posted April 18, 2009 Only thing there is tracking sessions. If session has been idle for > $threshold, increment counter by 1 and close session. Wouldn't that require page refreshes for the php to check? Also how would the 1x1 image be counted for exactly? Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-813463 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-813491 Share on other sites More sharing options...
gizmola Posted April 19, 2009 Share Posted April 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-813496 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 right. ajax is one method that was mentioned, and it it does have its own advantages. He just asked about doing it with an image, so I explained it. The main advantage to doing it with an image is that it will work even if the user disables javascript. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-813502 Share on other sites More sharing options...
gizmola Posted April 19, 2009 Share Posted April 19, 2009 CV, Yes, didn't mean to gloss over what you said -- was just trying to provide a few more details about the ajax. The meta - refresh iframe sounds like a good hack. Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-813601 Share on other sites More sharing options...
keeB Posted April 19, 2009 Share Posted April 19, 2009 Invisible iframes ftw Quote Link to comment https://forums.phpfreaks.com/topic/154585-ive-php/#findComment-813603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.