设置cookie wih JS,读取PHP问题

时间:2022-08-23 10:33:27

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

我试图用javascript设置一个cookie,然后用php在另一个页面中读取它。我能用行动来写饼干

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.

和我的部分工作。- cookie是编写的,我可以用$_COOKIE[cookieName]读取它,但只能在同一个web页面。

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?

这真的不是很有用。我需要再读一页。我通常在asp.net和c#中开发,所以我对php很陌生。我做错什么了吗?

Thank you for your time!

谢谢您的时间!

EDIT1: both pages are in the same domain.. eg. site.com/index.php -> site.com/index2.php

两个页面都在同一个域中。如。site.com/index.php - > site.com/index2.php

EDIT2: the cookie is set in one page through:

cookie设置为一页,通过:

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

EDIT3:我尝试设置域和添加路径= 到javascript代码…还没有. .

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

EDIT5:哦. .我的. .神……一直是个错字……只需要删除“path=/”+”;多姆…“我现在很惭愧……”与此同时,我也重新设置了我的饼干,所以现在我不走运,我不能接受你的文章。我带羞愧在我的名字! ! ! ....

4 个解决方案

#1


27  

Read on setting Javascript cookies and in particular path and domain access here:

请阅读下面的Javascript cookie,特别是路径和域访问:

http://www.quirksmode.org/js/cookies.html

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. 您没有从相同的域/子域和/或访问cookie
  3. the other page is not part of the path the cookie has specified.
  4. 另一个页面不是cookie指定的路径的一部分。

So your cookie is not giving the relevant information to the browser for it to be accessible across subdomains and/or the directory path.

因此,您的cookie并没有向浏览器提供相关信息,以便跨子域和/或目录路径访问它。

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:

注意,.example.com只是一个示例域(您需要您的域在其中),并且除了初始域之外不需要通配符。因为它可以遍历所有子域。您需要生成一个expires= date。从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.

我将定义域= bit添加到奇克斯莫德函数中。

EDIT (The example below originally referenced pages on my personal website.)

编辑(下面的示例最初引用了我个人网站上的页面)。

Andrej, this works perfectly fine for me:

Andrej,这对我来说很好:

http://example.com/test.php

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

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.

$_COOKIE的打印输出将显示该cookie。注意,当我检查cookie时,.example.com被正确设置为域。

#2


5  

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

只能通过在同一域中运行的代码访问cookie。

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

如果你的PHP和。net代码运行在不同领域然后浏览器不会发送cookie域和一个请求域B(反之亦然),同源策略的实施有助于保持网络快(因为浏览器不需要传递一个用户的所有饼干为每个请求和服务器不需要通过字节的无用的数据进行分析以获得它感兴趣的两个字段)和安全(因为我可以劫持您的帐户如果我有一块饼干有一个安全的会话密钥)

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

@Jared发布了一个来自QuirksMode的很好的链接,它提供了一个关于设置路径和域的很好的概述,这样你就可以在你的站点中获得你想要的访问权限。

#3


2  

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?

你用来创建cookie的唯一一行?

#4


2  

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

如果要扩展cookie的可用域,需要将其指定为cookie的一部分:

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

#1


27  

Read on setting Javascript cookies and in particular path and domain access here:

请阅读下面的Javascript cookie,特别是路径和域访问:

http://www.quirksmode.org/js/cookies.html

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. 您没有从相同的域/子域和/或访问cookie
  3. the other page is not part of the path the cookie has specified.
  4. 另一个页面不是cookie指定的路径的一部分。

So your cookie is not giving the relevant information to the browser for it to be accessible across subdomains and/or the directory path.

因此,您的cookie并没有向浏览器提供相关信息,以便跨子域和/或目录路径访问它。

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:

注意,.example.com只是一个示例域(您需要您的域在其中),并且除了初始域之外不需要通配符。因为它可以遍历所有子域。您需要生成一个expires= date。从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.

我将定义域= bit添加到奇克斯莫德函数中。

EDIT (The example below originally referenced pages on my personal website.)

编辑(下面的示例最初引用了我个人网站上的页面)。

Andrej, this works perfectly fine for me:

Andrej,这对我来说很好:

http://example.com/test.php

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

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.

$_COOKIE的打印输出将显示该cookie。注意,当我检查cookie时,.example.com被正确设置为域。

#2


5  

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

只能通过在同一域中运行的代码访问cookie。

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

如果你的PHP和。net代码运行在不同领域然后浏览器不会发送cookie域和一个请求域B(反之亦然),同源策略的实施有助于保持网络快(因为浏览器不需要传递一个用户的所有饼干为每个请求和服务器不需要通过字节的无用的数据进行分析以获得它感兴趣的两个字段)和安全(因为我可以劫持您的帐户如果我有一块饼干有一个安全的会话密钥)

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

@Jared发布了一个来自QuirksMode的很好的链接,它提供了一个关于设置路径和域的很好的概述,这样你就可以在你的站点中获得你想要的访问权限。

#3


2  

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?

你用来创建cookie的唯一一行?

#4


2  

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

如果要扩展cookie的可用域,需要将其指定为cookie的一部分:

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