Home » Php » php – setcookie() does not set cookie in Google Chrome

php – setcookie() does not set cookie in Google Chrome

Posted by: admin April 23, 2020 Leave a comment


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:

setcookie("user", "Alex Porter", time()+3600);

And from this site (for localhost environments):

setcookie("username", "George", false, "/", false);

Thanks in advance.

How to&Answers:

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 localhost.com).
  • Use http://myproject.localhacks.com/ (which points to
  • Use an empty domain value when setting the cookie.

For example, in PHP:

  time() + cookie_expiration(),

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

setcookie('test', 'This is a test', time() + 3600);
$cookieSet = 'The cookie is ' . $_COOKIE['test'];
} else {
$cookieSet = 'No cookie has been set';


echo $cookieSet;


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


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.


This code is working for me in IE, Chrome and FF

if($_COOKIE['language']==NULL || empty($_COOKIE['language']))

    $dirname = rtrim(dirname($_SERVER['PHP_SELF']), '/').'/';


    setcookie("language", "English",$expire,"$dirname","mydomain.com",false,false);