简体   繁体   English

将Javascript变量附加到Cookie

[英]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: 我的页面上有一个JavaScript计数器,尝试将其附加到Cookie( 关于此问题的我的第一个问题 ),这样,如果用户刷新页面,计数器就不会再从0开始:

page 1: 第1页:

<head>
<script type="text/javascript">
var pageVisisted = new Date(); 
setInterval(function() {
    var timeOnSite = new Date() - 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);
</script>
<?php
setcookie(AboutVisit, pageVisisted);
session_start();
?>
</head>

Page2: 第2页:

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

But then I get the display: pageVisisted 但是然后我得到显示:pageVisisted

setcookie(AboutVisit, pageVisisted); will not capture the value of pageVisited because that is a JavaScript variable. 不会捕获pageVisited的值,因为这是一个JavaScript变量。 JavaScript runs in a user's browser, and PHP has no awareness of it. JavaScript在用户的浏览器中运行,而PHP对此不了解。 PHP only knows about what's in its PHP blocks. PHP仅知道其PHP块中的内容。 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. 确实,就跟踪某人在您页面上的停留时间而言,您不需要涉及PHP,除非您需要在服务器上使用该信息。 Instead, your JavaScript can be responsible for maintaining your cookie. 相反,您的JavaScript可以负责维护cookie。 Unfortunately, JavaScript by itself has a horrible and counter-intuitive API for dealing with cookies. 不幸的是,JavaScript本身具有处理Cookie的可怕且违反直觉的API。 The article linked to by @vascowhite has a good set of helper JavaScript functions that you can use to simplify things. @vascowhite链接的文章提供了一组很好的辅助JavaScript函数,您可以使用它们来简化操作。 Here's what your JavaScript could look like with the addition of some of those helper functions: 通过添加一些辅助功能,您的JavaScript可能如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM