php SetCookie 在 Firefox 中有效,但在 IE 中无效

发布于 2024-09-11 01:05:37 字数 646 浏览 11 评论 0原文

我有两个 php 脚本

test.php

<?php
 header("location: test2.php");
 setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
 exit;
?>

test2.php

<?php
 var_dump($_COOKIE);
?>

然后我将浏览器指向 test.php,它重定向到 test2.php。然后我得到以下结果。

在 Firefox 中,我得到以下信息:

array
  'test' => string '8kFL4IZfjkBmV7AC' (length=16)

但是在 IE6 中,我得到以下信息:

array
  'PHPSESSID' => string 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' (length=32)

注意:我故意 X 掉了上面的 PHPSESSID!

有谁知道我哪里出了问题以及为什么 IE6 不显示我的 cookie。

提前致谢

I have two php scripts

test.php

<?php
 header("location: test2.php");
 setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
 exit;
?>

test2.php

<?php
 var_dump($_COOKIE);
?>

I then point my browser to test.php which redirects to test2.php. I then get the following results.

In firefox, i get the following:

array
  'test' => string '8kFL4IZfjkBmV7AC' (length=16)

However in IE6, i get the following:

array
  'PHPSESSID' => string 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' (length=32)

note: i have purposely X'd out the PHPSESSID above!

Does anybody know where i am going wrong and why IE6 isnt showing my cookie.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卖梦商人 2024-09-18 01:05:41

某些浏览器会在用户交互发生之前阻止设置 cookie。我知道 Safari 是这样的,而且我相信 IE 也有同样的工作方式。基本上,在从您的网站收到的第一个响应中,所有 cookie 都将被忽略。我怀疑如果您尝试类似以下内容,它将按预期工作:

test0.html

<html>
  <body>
    <a href="test1.php">force user interaction</a>
  </body>
</html>

test1.php

<?php
  header("location: test2.php");
  setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
  exit;
?>

test2.php

<?php
  var_dump($_COOKIE);
?>

Some browsers prevent the setting of cookies before user interaction has occurred. I know Safari does, and I believe IE works the same way. Basically, all cookies will be ignored on the first response received from your site. I suspect that if you instead try something like the following, it will work as expected:

test0.html

<html>
  <body>
    <a href="test1.php">force user interaction</a>
  </body>
</html>

test1.php

<?php
  header("location: test2.php");
  setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
  exit;
?>

test2.php

<?php
  var_dump($_COOKIE);
?>
沐歌 2024-09-18 01:05:40

我也有这个问题。我在 php 网站上从某人那里注意到了这一点。

在重定向的页面上设置 cookie 时,必须在调用 header('Location: ....'); 之后设置 cookie

http://php.net/manual/en/function.setcookie.php

我还是不确定

I also have this problem. I noticed this on the php website from somebody.

When setting a cookie on a page that redirects, the cookie must be set after the call to header('Location: ....');

http://php.net/manual/en/function.setcookie.php

I'm still unsure

墨离汐 2024-09-18 01:05:40

一个浏览器对您正在执行的标头重定向的反应可能比另一个浏览器更快。

尝试扭转命令:

 setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
 header("location: test2.php");

One browser could reacting more quickly to the header redirect you're doing then the other.

Try turning the commands around:

 setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
 header("location: test2.php");
‘画卷フ 2024-09-18 01:05:40

看看你的例子,你首先有 header() 然后是 setcookie()。
先尝试setcookie(),然后再执行header();

looking at your example, you have got header() first then setcookie().
Try and setcookie() first and then do the header();

梅倚清风 2024-09-18 01:05:39

您在本地主机环境中工作吗? IE http://localhost 进行测试?如果是这样,可能会导致设置的 cookie 出现一些问题。我的建议是设置 setcookie 的域字段,如果您在本地主机上工作,请尝试以下操作: setcookie("username", "George", false, "/", false); 或设置一个虚拟主机使用除 localhost 之外的服务器名称并将其用于域。

使用域设置 cookie 类似于:

setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/', '.domain.com');

希望如此帮助你。

Are you working on a localhost environment? IE http://localhost to test? If so this can cause some issues with the set cookie. My suggestion is setting the domain field for the setcookie, if you are working on localhost try this: setcookie("username", "George", false, "/", false); or set a vhost with a servername other than localhost and use that for the domain.

Setting the cookie with the domain would be something like:

setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/', '.domain.com');

Hopefully that helps ya out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文