35

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.

4
  • 7
    First problem "I tried the example from w3schools". You are better off just examining the PHP docs.
    – alex
    May 1, 2011 at 14:37
  • Do you have cookies enabled in Chrome? May 1, 2011 at 14:40
  • Did you try a cache-refresh (Shift+F5) to make sure it isn't just referencing a cached version. Or try opening the page "inPrivate" (Ctrl+Shift+P). See if you get the same results.
    – Jim
    May 1, 2011 at 14:53
  • @alex I just tried example 1 from the PHP docs (tried the 1st and 2nd setcookie lines) and results are the same. @tandu Cookies are definitely enabled, there are no exception in Chrome and sites that store cookies are working. @Brad F Jacobs Tried a hard refresh and clearing cache to be sure. Note: this also happens on Chrome in Linux as well.
    – Elyas
    May 1, 2011 at 15:16

6 Answers 6

41

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.

2
  • Brilliant! Thank you so much. I was having trouble with IE and Chrome, but not FF, Opera or Safari (on a WAMP system). I had tried almost everything. BTW - why this intentional design decision?
    – Diogenes
    May 1, 2011 at 21:30
  • 1
    Um, that's weird, though. I've been setting cookies on localhost with Chrome without any problems. Are you sure that "bug" doesn't just apply to setting them with JavaScript?
    – Dolda2000
    Jul 31, 2012 at 15:16
16

Domain should be equal to NULL.

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

setcookie("username", "George", time() + (20 * 365 * 24 * 60 * 60), "/", NULL);
1
  • 1
    This fixes the problem for me also, but I don't understand why? I thought it was standard practice to set a domain for cookies. Also for me it seems to depend on the server whether or not setting a cookie domain will work in Chrome Mar 2, 2015 at 23:55
5

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.

1
  • this is impossible scenario. You cannot access $_COOKIE['test'] from the same script that assign it, it won't be assigned unless the page has sent it's data to the browser. $_COOKIE['test'] is only assigned with each request from the browser. Jan 8, 2014 at 7:52
2

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

2

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.

1

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.