Javascript 用JS设置cookie,用PHP读取问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5045053/
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
Set cookie wih JS, read with PHP problem
提问by Andrej
I'm trying to set a cookie with javascript and read it in an other page with php. I am able to write the cookie by doing
我正在尝试使用 javascript 设置一个 cookie,并使用 php 在另一个页面中读取它。我可以通过做来写cookie
document.cookie = cookieName+"="+cookieValue;
and i partially works. - The cookie is written, and I am able to read it with $_COOKIE[cookieName]
but ONLY in the same web page.
我部分工作。- cookie 已写入,我$_COOKIE[cookieName]
只能在同一网页中读取它。
Which is not quite usefull really. I need to read it in another page. I usually develop in asp.net and c#, so I'm preety new to php. Am I doing something wrong?
这真的不太有用。我需要在另一页阅读它。我通常在 asp.net 和 c# 中开发,所以我对 php 很陌生。难道我做错了什么?
Thank you for your time!
感谢您的时间!
EDIT1: both pages are in the same domain.. eg. site.com/index.php -> site.com/index2.php
EDIT1:两个页面都在同一个域中......例如。site.com/index.php -> site.com/index2.php
EDIT2: the cookie is set in one page through:
EDIT2:cookie 通过以下方式设置在一页中:
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
and in another page it can not be accessed, but in that same page it can...
在另一个页面中它无法访问,但在同一页面中它可以......
EDIT3:
i tried setting the domain and added path=<?php echo $_SERVER['HTTP_HOST']; ?>
to the javascript code... still nothing..
EDIT3:我尝试设置域并添加path=<?php echo $_SERVER['HTTP_HOST']; ?>
到 javascript 代码中......仍然没有......
EDIT4: so far I have..
EDIT4:到目前为止我有..
document.cookie = cookieName+"="+escape(cookieValue)+"; expires="+expire.toGMTString()+"; path=/"+"; domain=.<?php echo $_SERVER['HTTP_HOST']; ?>";
and still i can read the cookie ONLY from the same page..
我仍然只能从同一页面读取 cookie..
EDIT5: oh.. my .. god... it was a typo all along... just needed to remove the" path=/"+"; dom..." i am so ashamed of myself right about now... in the meantime i also reset my cookies, so Jared now i unfortuneatly can't accept your post as anwser... i brought shame upon my name!!!....
EDIT5:哦..我的..天啊......一直是一个错字......只需要删除“path=/ “+”;dom......”我现在为自己感到羞耻...... .同时我也重置了我的cookies,所以Jared现在我不幸的是不能接受你的帖子作为anwser......我给我的名字带来了耻辱!!!......
回答by Jared Farrish
Read on setting Javascript cookies and in particular path and domain access here:
在此处阅读设置 Javascript cookie,特别是路径和域访问:
http://www.quirksmode.org/js/cookies.html
http://www.quirksmode.org/js/cookies.html
I think what is happening is one of two things:
我认为正在发生的事情是两件事之一:
- You aren't accessing the cookie from the same domain/subdomain, and/or
- the other page is not part of the path the cookie has specified.
- 您不是从同一个域/子域访问 cookie,和/或
- 另一个页面不是 cookie 指定的路径的一部分。
So your cookie is not giving the relevant information to the browser for it to be accessible across subdomains and/or the directory path.
因此,您的 cookie 不会向浏览器提供相关信息,以便跨子域和/或目录路径访问它。
document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/; ;domain=.example.com'
Note, .example.com
is just an example domain (you need yours in there), and you do not need a wildcard other than the initial .
for it go across all subdomains. And you need to generate an expires=
date. From QuirksMode:
请注意,.example.com
这只是一个示例域(您需要在那里使用您的域),除了首字母之外,您不需要通配符,.
因为它可以跨越所有子域。你需要生成一个expires=
日期。来自 QuirksMode:
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=/; domain=.example.com";
}
I added the domain=
bit to QuirksMode's function.
我在domain=
QuirksMode 的函数中添加了这个位。
EDIT(The example below originally referenced pages on my personal website.)
编辑(下面的示例最初引用了我个人网站上的页面。)
Andrej, this works perfectly fine for me:
安德烈,这对我来说非常好:
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=/; domain=.example.com";
}
createCookie('cookieee','stuff','22');
http://example.com/test/test.php
http://example.com/test/test.php
<pre>
<?php
print_r($_COOKIE);
?>
And the printout of $_COOKIE
will show the cookie. Note I when I inspect the cookie the .example.com is set correctly as the domain.
打印输出$_COOKIE
将显示 cookie。请注意,当我检查 cookie 时,.example.com 被正确设置为域。
回答by Sean Vieira
Cookies can only be accessed by code that is being run on the same domain.
Cookie 只能由在同一域上运行的代码访问。
If your PHP and .NET code are running on different domains then the browser will not send the cookies for Domain A with a request to Domain B (or vice versa) -- this enforcement of the Same-Origin Policyhelps keep the web fast (since the browser doesn't have to transmit allof a user's cookies for every request and the server doesn't have to parse through megabytes of useless data to get the two fields it is interested in) and secure (since I can hiHyman youraccount if I have a cookie from you with a secure session key.)
如果您的 PHP 和 .NET 代码在不同的域上运行,那么浏览器不会将域 A 的 cookie 与域 B 的请求一起发送(反之亦然)——这种同源策略的实施有助于保持网络速度(因为浏览器不必为每个请求传输用户的所有cookie,而且服务器不必解析数兆字节的无用数据来获取它感兴趣的两个字段)和安全(因为我可以劫持你的帐户)如果我有一个带有安全会话密钥的 cookie。)
@Jared has posted a good link from QuirksModethat gives an excellentoverview of setting path and domain so you have exactly the access you want within your site.
@Jared 从QuirksMode发布了一个很好的链接,它提供了设置路径和域的出色概述,因此您可以在您的站点中准确地获得所需的访问权限。
回答by John Nickerson
Can you give some more information? Are they both on the same domain just different files? Is the line
你能提供更多信息吗?他们都在同一个域中只是不同的文件吗?是线
document.cookie = cookieName+"="+cookieValue;
the only line you're using for creating the cookie?
您用于创建 cookie 的唯一行?
回答by zzzzBov
If you want to expand the domain available to the cookie you need to specify it as part of the cookie:
如果要扩展 cookie 可用的域,则需要将其指定为 cookie 的一部分:
document.cookie = cookieName + '=' + cookieValue + '; path=/;';
回答by Tanvir Ahmed
You need to set path for the cookie.
您需要为 cookie 设置路径。
For example in javascript if you do not specify the cookie path, it is set with the current page's path.
例如在 javascript 中如果不指定 cookie 路径,则设置为当前页面的路径。
In JS:
在JS中:
For setting the cookie:
用于设置 cookie:
document.cookie = "key=value; expires=Fri, 03 Aug 2018 12:00:00 UTC; path=/";
In Php:
在 PHP 中:
For setting the cookie:
用于设置 cookie:
setcookie('key', 'value', (time() + (3600*2)), '/');
For accessing:
访问:
if (isset($_COOKIE['key'])) echo $_COOKIE['key'];