setcookie() does not set cookie in Google Chrome setcookie() does not set cookie in Google Chrome google-chrome google-chrome

setcookie() does not set cookie in Google Chrome


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.


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

<?phpsetcookie('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><?phpecho $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.