开发者

Set cookie wih JS, read with PHP problem

开发者 https://www.devze.com 2023-02-12 11:55 出处:网络
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

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:

  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.


Cookies can only be accessed by code that is being run on the same domain.

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 Policy helps keep the web fast (since the browser doesn't have to transmit all of 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 hijack your account if I have a cookie from you with a secure session key.)

@Jared has posted a good link from QuirksMode that gives an excellent overview of setting path and domain so you have exactly the access you want within your site.


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?


If you want to expand the domain available to the cookie you need to specify it as part of the cookie:

document.cookie = cookieName + '=' + cookieValue + '; path=/;';


You need to set path for the cookie.

For example in javascript if you do not specify the cookie path, it is set with the current page's path.

In JS:

For setting the cookie:

document.cookie = "key=value; expires=Fri, 03 Aug 2018 12:00:00 UTC; path=/";

In Php:

For setting the cookie:

setcookie('key', 'value', (time() + (3600*2)), '/');

For accessing:

if (isset($_COOKIE['key'])) echo $_COOKIE['key'];
0

精彩评论

暂无评论...
验证码 换一张
取 消