setcookie() does not set cookie in Google Chrome

一个人想着一个人 提交于 2019-11-27 03:44:37
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

Ways to work around the issue include:

  • 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.

For example, in 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.

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

John Chornelius

Domain should be equal to NULL.

& Should be given an expiry date. i.e.,

setcookie("username", "George", time() + (20 * 365 * 24 * 60 * 60), "/", NULL);

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):

<?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)!

Conclusion: cookies were being set, but Chrome's development tools can't see it for some reason.

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

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.

This code is working for me in IE, Chrome and 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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!