从 JavaScript 读取 PHP cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8001676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:20:22  来源:igfitidea点击:

Read a PHP cookie from JavaScript

phpjavascriptcookies

提问by Boardy

I have a PHP script which sets a cookie called user. I need to be able to read the value from this cookie in javascript.

我有一个 PHP 脚本,它设置了一个名为 user 的 cookie。我需要能够在 javascript 中从这个 cookie 中读取值。

Can this even be done.

这甚至可以做到。

Thanks for any help you can provide.

感谢您的任何帮助,您可以提供。

采纳答案by yasar

You can access your cookies with document.cookieCheck this link: http://www.w3schools.com/js/js_cookies.asp

您可以通过document.cookie检查此链接访问您的 cookie :http: //www.w3schools.com/js/js_cookies.asp

回答by Marc B

There's no such thing as a "PHP cookie" or a "javascript cookie". There's just cookies, which you can access from PHP and Javascript. In JS, you'd use document.cookieto access the raw cookie data. There's plenty of libraries which give you finer-grained access as well,

没有“PHP cookie”或“javascript cookie”之类的东西。只有 cookie,您可以从 PHP 和 Javascript 访问它们。在 JS 中,您将用于document.cookie访问原始 cookie 数据。有很多库也可以让您进行更细粒度的访问,

回答by Brad

Take a look at document.cookie.

看看document.cookie

Example from http://www.quirksmode.org/js/cookies.html

来自http://www.quirksmode.org/js/cookies.html 的示例

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toUTCString();
    }
    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;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


Alternatively, if you use jQuery, there is a nice plugin: http://plugins.jquery.com/project/Cookie

或者,如果您使用 jQuery,有一个不错的插件:http: //plugins.jquery.com/project/Cookie

回答by webworker

I tested it. You can read cookie which created with php.

我测试了它。您可以读取用 php 创建的 cookie。

You only need to add '/' (path)!!

您只需要添加'/'(路径)!!

setcookie("username", $username, $expire, '/');

回答by Patrick Moore

Whether the cookie originates in JS or PHP should not matter. The cookie is stored for the domain, with a name and value. It does not contain information relating to how it originated.

cookie 是源自 JS 还是 PHP 无关紧要。cookie 是为域存储的,具有名称和值。它不包含有关其起源的信息。

Here is a plugin for accessing cookies in jQuery: http://plugins.jquery.com/project/Cookie

这是一个在 jQuery 中访问 c​​ookie 的插件:http: //plugins.jquery.com/project/Cookie

回答by rogerlsmith

Since PHP is a server side script and JavaScript runs in the browser, you'll need to send the cookie to the browser in a hidden form variable, or use an AJAX call from JavaScript to get it from the server.

由于 PHP 是服务器端脚本并且 JavaScript 在浏览器中运行,因此您需要以隐藏的表单变量将 cookie 发送到浏览器,或者使用来自 JavaScript 的 AJAX 调用从服务器获取它。