Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

javascript - Set cookie wih JS, read with PHP problem

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!!!....

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. You aren't accessing the cookie from the same domain/subdomain, and/or
  2. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...