设置cookie wih JS,读取PHP问题 [英] Set cookie wih JS, read with PHP problem
问题描述
我尝试用javascript设置cookie,并在php的其他页面中读取。
我可以通过执行
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
document.cookie = cookieName+"="+cookieValue;
和i部分工作。 - 写的cookie,我可以用 $ _ COOKIE [cookieName]
阅读,但只能在同一个网页上。
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.
这真的不是很有用。我需要在另一个页面读它。我通常在asp.net和c#开发,所以我preper新的php。我会做错事吗?
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?
感谢您的时间!
EDIT1:
在同一个域。 site.com/index.php - > site.com/index2.php
both pages are in the same domain.. eg. site.com/index.php -> site.com/index2.php
EDIT2:
在一页中设置Cookie:
the cookie is set in one page through:
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尝试设置域并添加 path =<?php echo $ _SERVER ['HTTP_HOST' ];
i tried setting the domain and added path=<?php echo $_SERVER['HTTP_HOST']; ?>
to the javascript code... still nothing..
EDIT4:
到目前为止我有..
so far I have..
document.cookie = cookieName+"="+escape(cookieValue)+"; expires="+expire.toGMTString()+"; path=/"+"; domain=.<?php echo $_SERVER['HTTP_HOST']; ?>";
,仍然可以从同一页面读取cookie。
and still i can read the cookie ONLY from the same page..
EDIT5:
oh .. my .. god ...这是一个错字,只是需要删除path = / +<强...>我很羞愧自己现在...
在此期间我还重置了我的饼干,所以Jared现在我不幸地不能接受你的帖子作为anwser ... $
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!!!....
推荐答案
阅读设置Javascript Cookie,特别是路径和域访问权限:
Read on setting Javascript cookies and in particular path and domain access here:
http://www.quirksmode.org /js/cookies.html
我认为发生的是以下两种情况之一:
I think what is happening is one of two things:
- 您不是从同一个域/子域访问Cookie,和/或
- 另一个网页不是Cookie指定的路径的一部分。
因此,您的Cookie未向浏览器提供相关信息,以便通过子域和/或目录路径访问。 / p>
So your cookie is not giving the relevant information to the browser for it to be accessible across subdomains and/or the directory path.
document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/; ;domain=.example.com'
注意, .example.com
只是一个示例域(您需要您的域),并且不需要除了初始之外的通配符。
。您需要生成 expires =
日期。从QuirksMode:
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:
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";
}
我添加了 domain = $ c $
I added the domain=
bit to QuirksMode's function.
EDIT (我的个人网站上最初引用的网页示例)
EDIT (The example below originally referenced pages on my personal website.)
Andrej,对我来说完全正常:
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
<pre>
<?php
print_r($_COOKIE);
?>
并且 $ _ COOKIE
的打印输出cookie。注意我在检查Cookie时,将.example.com设置为域正确。
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 wih JS,读取PHP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!