0

I have one page with 5 sub pages (each one has a different page link) what I am trying to do is create a timer to count how many seconds a user spend on a phone call.

for example

main.php  << the landing page
tab1.php  << additional tab
tab2.php  << additional tab
tab3.php  << additional tab
tab4.php  << additional tab

the first time around the use clicks on the landing page "main.php" here I need to start a counter and if the user click on tab 1, tab2, tab3 or tab 4 I want the timer to still count the seconds that the user spend on those page.

I have used a jQuery plugin called "runner" http://plugins.jquery.com/runner/ to have a counter on my page but when the user switch to a different tab the counter is reset. My only problem is how can I create a counter that carries the value to the other tabs without losing the counter.

Question Summery The core of my question is how to build a live counter in cookies? so that the cookie value will be incremented by 1 every second. the it will not stop until I clear the cookie in a different page.

I am using PHP to do the rest of the code

I appreciate your help.

Mike
  • 2,735
  • 11
  • 44
  • 68
  • 1
    Have you tried to do anything to implement this functionality? What is the specific problem you are having (i.e. do you need to know how to write cookies in javascript?) – Mike Brant Apr 08 '13 at 19:36
  • 1
    Do you want to differentiate how long a user is on each page? Or do you just want to know how long it takes from first hitting the landing page to the end of the last page? – Mercurybullet Apr 08 '13 at 19:38
  • Store the start-time in a cookie, then when you want to know how long it has been going, compare current time to start time. – Kevin B Apr 08 '13 at 19:47
  • The problem that I am having is to keep a live counter in the cookies. so each second goes by I need it to be stored to a cookie variable so I can retrieve it when ever i need. – Mike Apr 08 '13 at 19:52

2 Answers2

0

You can store start time in your cookie and read end time before clearing it. Or use a session to do this?

    if(!isset($_SESSION['start'] ))$_SESSION['start'] = time(); //on your landing page 

    $time_spent = time()-$_SESSION['start']; //on your tabs 
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • Thanks for that code. This code check if the session exists but it does not show me how to keep the time ticker going – Mike Apr 08 '13 at 20:23
  • Your javascript counter has a startAt parameter. You can use that to start at time `$time_spent`. `echo '$(\'#runner\').runner({ countdown: true, startAt: $time_spent,`. Note startAt is in miliseconds, so use [microtime]http://php.net/manual/en/function.microtime.php – Bass Jobsen Apr 08 '13 at 20:41
  • yes I am aware of that. But What I am having diffucultiey with is to keep the counter going in the cookies and not in a div. I need to know how to reassign the cookie value every seacond – Mike Apr 08 '13 at 21:18
  • `setCookie('timespent',0,365); window.setInterval(function() {setCookie('timespent', parseInt(getCookie('timespent'))+1,365)}, 1000);` and use the set/get cookie from b.e. [javascript cookie]http://www.hardcode.nl/subcategory_1/article_314-get-cookie-and-set-cookie.htm – Bass Jobsen Apr 08 '13 at 22:19
0

You might be looking for a way to set a cookie with Javascript. If you really want to, you can call createCookie() every second. You will also have to keep track of the start time to get the actual time spent on the page. But this is just to maybe help you get on track. There are definitely better approaches anyways. Just listen to the other comments.

var unixTimestamp = Math.round(new Date().getTime() / 1000);
window.setInterval("createCookie('myCookie', unixTimestamp)", 1000);    

function createCookie(c_name, value) {
    var expires = "";
    document.cookie = c_name + "=" + value + expires + "; path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

As already discussed and published on https://stackoverflow.com/a/4825695/407697

Community
  • 1
  • 1
sprain
  • 7,552
  • 6
  • 35
  • 49