setcookie() 不会在 Google Chrome 中设置 cookie
我正在阅读一些有关如何设置 cookie 的 PHP 教程。我注意到 cookie 在 FF4 和 IE9 上成功设置,但在 Chrome (11.0.696.60) 中没有设置。 PHP 文件由 XAMPP (localhost) 提供。
我尝试了 w3schools 中的示例:
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
以及 此站点 (对于本地主机环境):
<?php
setcookie("username", "George", false, "/", false);
?>
提前致谢。
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).
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);
?>
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
禁用 IP 地址和 localhost 的 cookie 是一项设计决策。另请参阅:https://code.google.com/p/chromium/ issues/detail?id=56211
解决该问题的方法包括:
/etc/hosts
以使用127.0.0.1 localhost.com< /代码>)。
例如,在 PHP 中:
这里的值
null
表示不应设置域。注意:不设置域会阻止 cookie 对子域可见。
Disabling cookies for IP addresses and
localhost
was a design decision. See also: https://code.google.com/p/chromium/issues/detail?id=56211Ways to work around the issue include:
/etc/hosts
to use127.0.0.1 localhost.com
).For example, in PHP:
Here the value
null
indicates that the domain should not be set.Note: not setting the domain prevents the cookie being visible to sub-domains.
域应等于 NULL。
&应该给出一个有效期限。 IE,
Domain should be equal to NULL.
& Should be given an expiry date. i.e.,
看来这可能是 Chrome 的“开发者工具”功能的一个错误。我一直在尝试设置 cookie(但不检索它)并且它可以与其他浏览器一起使用。假设您信任 FF 的 cookie 查看部分或找到 IE 的 cookie 文件,它就会起作用。在 Chrome 中,我依赖于“开发人员工具”的“Cookie”部分(开发人员工具 > 资源 > Cookie)。
我决定更进一步,使用 WHT 中找到的脚本实际输出 cookie 的值 (由 Natcoweb 发布):
它适用于所有浏览器,包括 Chrome(我得到:“Cookie 是这是一个测试”)!然而,Chrome 的 cookie 检查器继续显示“此网站没有 cookie”。我还设法找到了 Chrome 设置中存储的 cookie 列表(选项 > Under the Hood > 内容设置 > 所有 cookie 和站点数据)并最终找到了 cookie(检查步骤更多,但至少比开发人员工具更准确) )!
结论:cookie已被设置,但Chrome的开发工具由于某种原因看不到它。
It seems like this might be a bug with the "Developer Tools" feature of Chrome. The whole time I was trying to set a cookie (but not retrieve it) and it worked with the other browser. It worked, assuming you trust the cookie viewing section of FF or locate the cookie's file for IE. In Chrome I was relying on the "Cookies" section of the "Developers Tools" (Developer Tools > Resources > Cookies).
I decided to take a step further and actually output the cookie's value using this script found in WHT (posted by Natcoweb):
And it worked on all browsers including Chrome (I get: "The cookie is This is a test")! However Chrome's cookie inspector continues showing "This site has no cookies". I also managed to find the list of cookies stored in Chrome's settings (Options > Under the Hood > Content Settings > All cookies and site data) and finally found the cookie (more steps to check but at least more accurate than developer tools)!
Conclusion: cookies were being set, but Chrome's development tools can't see it for some reason.
你检查过你的系统日期吗?
$ 日期
如果时间已经很旧了,那么你应该改变你的时间
$ date -s 2007.04.08-22:46+0000
我希望这有帮助。我遇到了同样的问题并且有效
Did you check your system date?
$ date
And if it is old time then you should change your time
$ date -s 2007.04.08-22:46+0000
I hope this helps. I had the same problem and it worked
当我尝试如下时,我遇到了同样的问题
,当我将其更改为下面时,
我工作得很好,区别在于,我只是做了 time()+一些数值工作,而不是 time()+60*60*24*30 。我知道这没有意义,但它确实有效。
I faced the same issue when i tried as below
when i changed it to below
I just worked fine, the difference is instead of time()+60*60*24*30 i just did time()+some numeric value work. I know that doesn't make sense but it worked.
这段代码适用于 IE、Chrome 和 FF
This code is working for me in IE, Chrome and FF