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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…