用JS设置cookie,用PHP读取问题

时间:2023-01-17
本文介绍了用JS设置cookie,用PHP读取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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

我认为正在发生的事情是两件事之一:

  1. 您不是从同一个域/子域访问 cookie,和/或
  2. 另一个页面不是 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:

  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.

这篇关于用JS设置cookie,用PHP读取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 cookie 中存储 PHP 数组 下一篇:如何在php中删除我网站的所有cookie

相关文章

最新文章