This sort of thing is actually quite complicated; as there is no requirement from a browser to "keep in touch" with the web server with regards to how long a page is open (and even if it's open, you've no idea that it's actually being actively used by an end user - it could be open in another tab for instance).
The most basic method would be to identify time spent on a given page whilst navigating around your site - that can be done quite easily using cookies and keeping tracking of the time a page was requested and the last page view URL. Then, in your header of each page you can check for the "lastview" cookie and if set; record the time taken viewing that page as now() - lastviewtime (a separate cookie).
A more complex approach, but would require constant callback to your server via "AJAX" might be to setup a JavaScript timer function to "ping" your server to indicate that a page was still being viewed say, every second - but this of course relies on JavaScript....
Hope this points you in the right direction!
Cheers,
David.
Hi David,
Want to thank you for this. I guess I need to figure out away to do this, due to our radio ad network, In the database we have the seconds of each mp3 we are just trying to make sure radio stations don't play the ad for like 1 second and then stop the ad and go on to music.
Hi Russell, This sort of
Hi Russell,
This sort of thing is actually quite complicated; as there is no requirement from a browser to "keep in touch" with the web server with regards to how long a page is open (and even if it's open, you've no idea that it's actually being actively used by an end user - it could be open in another tab for instance).
The most basic method would be to identify time spent on a given page whilst navigating around your site - that can be done quite easily using cookies and keeping tracking of the time a page was requested and the last page view URL. Then, in your header of each page you can check for the "lastview" cookie and if set; record the time taken viewing that page as now() - lastviewtime (a separate cookie).
A more complex approach, but would require constant callback to your server via "AJAX" might be to setup a JavaScript timer function to "ping" your server to indicate that a page was still being viewed say, every second - but this of course relies on JavaScript....
Hope this points you in the right direction!
Cheers,
David.
Hi David, Want to thank you
Hi David,
Want to thank you for this. I guess I need to figure out away to do this, due to our radio ad network, In the database we have the seconds of each mp3 we are just trying to make sure radio stations don't play the ad for like 1 second and then stop the ad and go on to music.
Here is the main part of the code I am using
<?php$track = "ad.mp3";
if (file_exists($track)) {
header("Content-Type: audio/mpeg");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: inline; filename="iinet.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($track);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
?>