Page 1 of 1

Record and Display Amount of Time Spent on Page

Posted: Fri Aug 29, 2008 2:03 pm
by eviljoker7075
Hey there everyone! My first post here how you all doing!?

Ok, so I'm quite new to PHP (very new), but have tried to work through this, but just cannot find any information to help. Basically I need to design a way to record the amount of time a user spends on a page, and then display that time on another page when they click for it.

I'm guessing a good way to do this would be using a cookie as it will only need to be a one time display of the information and no record needs to be kept for my own use - past me being nosey of course!

I'm hoping someone can help me get something like this going, or suggest a better alternative. Thanks for reading and I hope to hear from someone soon :)

Kev

Re: Record and Display Amount of Time Spent on Page

Posted: Fri Aug 29, 2008 3:26 pm
by The_Anomaly
I'm relatively new as well, so I'm sure others will give you far better suggestions. A few weeks ago I was working with mod_rewrite, and the MVC protocol. Basically, everytime the user requested a page, it would go to the same page, which would direct the visitor to the requested page. This was called a bootstrap, and implemented with an .htaccess file, and mod_rewrite.

Perhaps if such a site structure were implemented, you could log the time between requests for a certain user. Say a user comes to your site, he's assigned a session ID, or some identifying cookie. Every time the user requests a page, the time and page requested is logged. The time between the pages for that individual session ID would be how much time he spent on that page.

Just a thought I had--as I said before, I'm sure there are much better ways, perhaps implementing some javascript timer for each page, or something else.

EDIT: But of course, if the user closes the browser, it would screw everything up ;) Javascript is probably the way to go, as the below poster pointed out.

Re: Record and Display Amount of Time Spent on Page

Posted: Fri Aug 29, 2008 3:35 pm
by DaiWelsh
This cannot be done with PHP alone as the PHP code just generates a page and sends it to the browser, how long the browser spends on that page has no impact on PHP. The only practical solutions involve javascript, but even then you need to think about what you mean by time spent on a page. e.g. if I load a page then switch to word and write an essay I am still on that page as far as the browser is concerned but clearly not readin/using it. Likewise if I browse away from the page then come back to it, is that counted as time on the page or a fresh visit?

If you are really sure you still want to do it then explore one of two routes; either the window.onUnload() method (which should allow you to take some action when your page is unloaded) or some form of background ajax polling code that "pings" a PHP script on your server every few seconds as long as the page remains open. The former can be a bit flaky in my experience and if you get it wrong could really annoy the user (pages that refuse to be closed anyone?) whereas the latter adds to the background bandwidth but if done efficiently could work quite well.

It should even be possible with the polling method to tell if the user moves focus away from the window (window.onBlur() method), though again I would use that with extreme caution if you like your users ;)

P.S. Time between requests != time on a page by any stretch of the imagination, sorry :)

Regards,

Dai

Re: Record and Display Amount of Time Spent on Page

Posted: Fri Aug 29, 2008 5:07 pm
by eviljoker7075
Ok, I'm not gonna pretend to understand the majority of what you said! Sorry.

Javascript sounds like the way to go then. I'm not too worried about users returning to the page at a later date, and in fact I am not worried about if they spend the whole time on my page. That is why I suggested calculating the difference in time between when the page was opened/loaded to when it was closed/navigated away from.

Given that would you still suggest using JavaScript in some way, and if so, I know it's a pain to have to hold someones hand like this, but could you start me off?

Thanks for the advice so far.

Re: Record and Display Amount of Time Spent on Page

Posted: Fri Aug 29, 2008 5:28 pm
by eviljoker7075
Ok, I did a little further searching with the info you gave me and I've made a start, I now have a script that does exactly what I mentioned at the start... It determines the difference between load time and leave time. However now I am stuck as to how to display the information on another page. Currently the time is displayed via an alert box. I have included my page code below, where do I go from here...?

Code: Select all

<script type="text/javascript">
function person_in(){enter=new Date();}
function person_out(){
  exit=new Date();
  time_dif=(exit.getTime()-enter.getTime())/1000;
  time_dif=Math.round(time_dif);
  alert("You've been on this page for: "+time_dif+" seconds");
}
</script>
<body onLoad="person_in()" onUnload="person_out()">
&nbsp;

Re: Record and Display Amount of Time Spent on Page

Posted: Sun Aug 31, 2008 6:06 am
by eviljoker7075
Just to let you all know I now have a solution to this. I've included the code below in case any one else wants to use this system. Thanks for your help up to now though :)


Page 1

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
 
<script type="text/javascript">
function person_in(){enter=new Date();}
function person_out(){
var  exit=new Date();
var  time_dif=(exit.getTime()-enter.getTime())/1000;
  time_dif=Math.round(time_dif);
  alert("You've only been on this page for: "+time_dif+" seconds!!");
createCookie('myCookie', time_dif ,1*24*60*60*1000); // set for 1 day
}
 
function load(e) {
var enabled = Cookie.isEnabled();
if (!enabled)
alert('You must enable cookies.');
}
 
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
</script>
</head>
 
<body onLoad="person_in()" onUnload="person_out()">
<a href="page2.htm">link</a> 
</body>
</html>

Page 2

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
 
<script type="text/javascript">
function load(e) {
var enabled = Cookie.isEnabled();
if (!enabled)
alert('You must enable cookies.');
}
document.write(readCookie('myCookie')); 
 
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
</script>
</head>
 
<body>
</body>
</html>