問題描述
我正在嘗試使用 javascript 設置 cookie,并使用 php 在另一個頁面中讀取它.我可以通過做來寫 cookie
document.cookie = cookieName+"="+cookieValue;
我部分工作.- cookie 已寫入,我可以使用 $_COOKIE[cookieName]
讀取它,但只能在同一網頁中讀取.
這真的不太有用.我需要在另一頁閱讀它.我通常在 asp.net 和 c# 中開發,所以我對 php 很陌生.我做錯了什么嗎?
感謝您的寶貴時間!
編輯 1:兩個頁面都在同一個域中......例如.site.com/index.php -> site.com/index2.php
cookie 通過以下方式設置在一頁中:
function SetCookie(cookieName,cookieValue,nDays) {今天無功 = 新日期();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();}
在另一個頁面中它無法訪問,但在同一頁面中它可以......
我嘗試設置域并添加 path=<?php echo $_SERVER['HTTP_HOST'];?>
到 javascript 代碼...仍然什么都沒有..
到目前為止我有..
document.cookie = cookieName+"="+escape(cookieValue)+"; expires="+expire.toGMTString()+"; path=/"+"; domain=.<?php echo $_SERVER['HTTP_HOST']; ?>";
我仍然只能從同一頁面讀取 cookie..
哦..我的..天啊...一直是一個錯字...只需要刪除path=/"+;dom..."我為自己感到羞恥馬上...與此同時,我也重置了我的 cookie,所以 Jared 現在不幸地不能接受你的帖子作為 anwser...我給我的名字帶來了恥辱!!!....
在此處閱讀有關設置 Javascript cookie,特別是路徑和域訪問的信息:
http://www.quirksmode.org/js/cookies.html
我認為正在發生的事情是兩件事之一:
- 您不是從同一個域/子域訪問 cookie,和/或
- 另一個頁面不是 cookie 指定的路徑的一部分.
因此,您的 cookie 沒有向瀏覽器提供相關信息,以便它可以跨子域和/或目錄路徑訪問.
document.cookie = 'ppkcookie1=testcookie;expires=2001 年 8 月 2 日星期四 20:47:11 UTC;路徑=/;;域=.example.com'
注意,.example.com
只是一個示例域(您需要在那里使用您的域),除了初始 .
之外,您不需要通配符遍歷所有子域.并且您需要生成一個 expires=
日期.來自 QuirksMode:
function createCookie(name,value,days) {如果(天){var date = new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires = "; expires="+date.toGMTString();} 別的 {var expires = "";}document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";}
我在 QuirksMode 的函數中添加了 domain=
位.
編輯(以下示例最初引用了我個人網站上的頁面.)
Andrej,這對我來說非常好:
http://example.com/test.php
function createCookie(name,value,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
<?php打印_r($_COOKIE);?>
$_COOKIE
的打印輸出將顯示 cookie.請注意,當我檢查 cookie 時,.example.com 被正確設置為域.
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;
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.
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?
Thank you for your time!
EDIT1: both pages are in the same domain.. eg. site.com/index.php -> site.com/index2.php
EDIT2: 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 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']; ?>";
and still i can read the cookie ONLY from the same page..
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!!!....
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:
- 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.
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'
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";
}
I added the domain=
bit to QuirksMode's function.
EDIT (The example below originally referenced pages on my personal website.)
Andrej, this works perfectly fine for me:
http://example.com/test.php
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);
?>
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.
這篇關于用JS設置cookie,用PHP讀取問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!