setcookie() 不会在 Google Chrome 中设置 cookie [英] setcookie() does not set cookie in Google Chrome

查看:28
本文介绍了setcookie() 不会在 Google Chrome 中设置 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习一些关于如何设置 cookie 的 PHP 教程.我注意到在 FF4 和 IE9 上成功设置了 cookie,但是它没有在 Chrome (11.0.696.60) 中设置.PHP 文件由 XAMPP (localhost) 提供.

I am going through some PHP tutorials on how to set cookies. I have noticed that cookies are successfully set on FF4 and IE9, however it does not get set in Chrome (11.0.696.60). The PHP file was served from XAMPP (localhost).

我尝试了 w3schools 的例子:

I tried the example from w3schools:

<?php
setcookie("user", "Alex Porter", time()+3600);
?>

来自这个站点(对于本地主机环境):

And from this site (for localhost environments):

<?php
setcookie("username", "George", false, "/", false);
?>

提前致谢.

推荐答案

禁用 IP 地址和 localhost 的 cookie 是一个设计决策.另请参阅:https://code.google.com/p/chromium/问题/详细信息?id=56211

Disabling cookies for IP addresses and localhost was a design decision. See also: https://code.google.com/p/chromium/issues/detail?id=56211

解决该问题的方法包括:

Ways to work around the issue include:

  • 设置本地域(例如,编辑 /etc/hosts 以使用 127.0.0.1 localhost.com).
  • 使用 http://myproject.localhacks.com/(指向 127.0.0.1).
  • 设置 cookie 时使用空域值.
  • Set a local domain (e.g., edit /etc/hosts to use 127.0.0.1 localhost.com).
  • Use http://myproject.localhacks.com/ (which points to 127.0.0.1).
  • Use an empty domain value when setting the cookie.

例如,在PHP中:

setcookie(
  $AUTH_COOKIE_NAME,
  $cookie_value,
  time() + cookie_expiration(),
  $BASE_DIRECTORY,
  null,
  false,
  true
);

这里的值 null 表示不应设置域.

Here the value null indicates that the domain should not be set.

注意:不设置域会阻止 cookie 对子域可见.

Note: not setting the domain prevents the cookie being visible to sub-domains.

这篇关于setcookie() 不会在 Google Chrome 中设置 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆