php setcookie() 不会在 Google Chrome 中设置 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5849013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setcookie() does not set cookie in Google Chrome
提问by Elyas
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).
我正在阅读一些关于如何设置 cookie 的 PHP 教程。我注意到在 FF4 和 IE9 上成功设置了 cookie,但是它没有在 Chrome (11.0.696.60) 中设置。PHP 文件由 XAMPP (localhost) 提供。
I tried the example from w3schools:
我尝试了 w3schools 的例子:
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
And from this site(for localhost environments):
从这个站点(对于本地主机环境):
<?php
setcookie("username", "George", false, "/", false);
?>
Thanks in advance.
提前致谢。
回答by Mike West
Disabling cookies for IP addresses and localhost
was a design decision. See also: https://code.google.com/p/chromium/issues/detail?id=56211
禁用 IP 地址的 cookielocalhost
是一个设计决定。另见:https: //code.google.com/p/chromium/issues/detail?id=56211
Ways to work around the issue include:
解决该问题的方法包括:
- Set a local domain (e.g., edit
/etc/hosts
to use127.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.
- 设置本地域(例如,编辑
/etc/hosts
以使用127.0.0.1 localhost.com
)。 - 使用http://myproject.localhacks.com/(指向 127.0.0.1)。
- 设置 cookie 时使用空域值。
For example, in PHP:
例如,在PHP 中:
setcookie(
$AUTH_COOKIE_NAME,
$cookie_value,
time() + cookie_expiration(),
$BASE_DIRECTORY,
null,
false,
true
);
Here the value null
indicates that the domain should not be set.
这里的值null
表示不应设置域。
Note:not setting the domain prevents the cookie being visible to sub-domains.
注意:不设置域会阻止 cookie 对子域可见。
回答by John Chornelius
Domain should be equal to NULL.
域应等于 NULL。
& Should be given an expiry date. i.e.,
& 应该给出一个到期日期。IE,
setcookie("username", "George", time() + (20 * 365 * 24 * 60 * 60), "/", NULL);
回答by Elyas
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).
看起来这可能是 Chrome 的“开发人员工具”功能的一个错误。我一直在尝试设置 cookie(但不检索它)并且它与其他浏览器一起工作。它有效,假设您信任 FF 的 cookie 查看部分或为 IE 找到 cookie 的文件。在 Chrome 中,我依赖于“开发人员工具”(开发人员工具 > 资源 > Cookies)的“Cookies”部分。
I decided to take a step further and actually output the cookie's value using this script found in WHT (posted by Natcoweb):
我决定更进一步,使用在 WHT 中找到的这个脚本(由 Natcoweb 发布)实际输出 cookie 的值:
<?php
setcookie('test', 'This is a test', time() + 3600);
if(isset($_COOKIE['test'])){
$cookieSet = 'The cookie is ' . $_COOKIE['test'];
} else {
$cookieSet = 'No cookie has been set';
}
?>
<html>
<head><title>cookie</title></head>
<body>
<?php
echo $cookieSet;
?>
</body>
</html>
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)!
它适用于包括 Chrome 在内的所有浏览器(我得到:“cookie 是这是一个测试”)!但是 Chrome 的 cookie 检查器继续显示“此站点没有 cookie”。我还设法找到了存储在 Chrome 设置中的 cookie 列表(选项 > 引擎盖下 > 内容设置 > 所有 cookie 和站点数据),最后找到了 cookie(检查的步骤更多,但至少比开发人员工具更准确)!
Conclusion: cookies were being set, but Chrome's development tools can't see it for some reason.
结论:cookie 正在设置,但由于某些原因,Chrome 的开发工具看不到它。
回答by Sahin Yanl?k
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
你检查你的系统日期了吗?$ date 如果它是旧时间,那么你应该改变你的时间 $ date -s 2007.04.08-22:46+0000
I hope this helps. I had the same problem and it worked
我希望这有帮助。我遇到了同样的问题并且它有效
回答by Satya
I faced the same issue when i tried as below
当我尝试如下时,我遇到了同样的问题
setcookie("gb_role",base64_encode($_SESSION["role"]),time()+60*60*24*30);
when i changed it to below
当我把它改成下面
setcookie("gb_role",base64_encode($_SESSION["role"]),time()+2592000);
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.
我工作得很好,不同之处在于我只是做了 time()+一些数值工作,而不是 time()+60*60*24*30。我知道这没有意义,但它奏效了。
回答by Dorar
This code is working for me in IE, Chrome and FF
这段代码在 IE、Chrome 和 FF 中对我有用
if($_COOKIE['language']==NULL || empty($_COOKIE['language']))
{
$dirname = rtrim(dirname($_SERVER['PHP_SELF']), '/').'/';
$expire=time()+31536000;
setcookie("language", "English",$expire,"$dirname","mydomain.com",false,false);
}