开发者

Attach Javascript variable to cookie

I've been strugling, maybe someone can help me please? I've got a javascript counter on my page, trying to attach it to a cookie (My first quetion about this) so that if the user refreshes the page the counter does not start from 0 again:

page 1:

<head>
<script type="text/javascript">
var pageVisisted = new Date(); 
setInterval(function() {
    var timeOnSite = new Date() - pageV开发者_运维百科isisted;
    var secondsTotal = timeOnSite / 1000;
    var hours = Math.floor(secondsTotal / 3600);
    var minutes = Math.floor(secondsTotal / 60) % 3600;
    var seconds = Math.floor(secondsTotal)  % 60;
    document.getElementById('counter').innerHTML = hours + ":" + minutes + ":" + seconds;
}, 1000);
</script>
<?php
setcookie(AboutVisit, pageVisisted);
session_start();
?>
</head>

Page2:

$cookid = $_COOKIE['AboutVisit'];
echo $cookid;

But then I get the display: pageVisisted


setcookie(AboutVisit, pageVisisted); will not capture the value of pageVisited because that is a JavaScript variable. JavaScript runs in a user's browser, and PHP has no awareness of it. PHP only knows about what's in its PHP blocks. In other words, it sees your code like this:

Arbitrary text
<?php
setcookie(AboutVisit, pageVisisted); // What is pageVisited?!
session_start();
?>
Arbitrary text

Really, as far as tracking how long someone is on your page goes, you don't need to involve PHP unless you need to use that information on the server. Instead, your JavaScript can be responsible for maintaining your cookie. Unfortunately, JavaScript by itself has a horrible and counter-intuitive API for dealing with cookies. The article linked to by @vascowhite has a good set of helper JavaScript functions that you can use to simplify things. Here's what your JavaScript could look like with the addition of some of those helper functions:

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=/";
}

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;
}

// Read the cookie; if it's not there, start at the current date
var cookieVal = readCookie('AboutVisit');
var pageVisisted = cookieVal ? parseInt(cookieVal) : new Date().milliseconds;
if (!cookieVal) {
    createCookie('AboutVisit', pageVisited.toString());
}

setInterval(function() {
    var timeOnSite = new Date().milliseconds - pageVisisted;
    var secondsTotal = timeOnSite / 1000;
    var hours = Math.floor(secondsTotal / 3600);
    var minutes = Math.floor(secondsTotal / 60) % 3600;
    var seconds = Math.floor(secondsTotal)  % 60;
    document.getElementById('counter').innerHTML = 
        hours + ":" + minutes + ":" + seconds;
}, 1000);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜