blex blex - 1 month ago 8x
PHP Question

Cookie is present in browser, but php $_COOKIE is empty

I have a project that works on a local server but not on my production server, due to cookies not being seen by the server. I've made a minimal version of the code that reproduces the issue on that server:


setcookie('foo', 'bar', time() + 7*24*60*60, '/');
echo "Cookie was not found, so we just created it.";
} else {
echo "Cookie was found!";


No matter how many times I refresh this page, I always get the "not found" message. Whenever I try to log the
variable, I get an empty Array. However:

  • The cookie is present in the browser, and correctly sent with the request

  • The cookie is set and read in the same file (it's not an issue with the

  • There is no output before
    , and the file is encoded in UTF8 without BOM

I think this is a server configuration issue, since the code works locally, but I have no idea where to look. Has anyone seen this before, do you know what could cause this?

If you need more info, just tell me and I'll add it to my question. Thank you!


If there's a cache server or CDN involved, it may be filtering cookies based on a whitelist. This is to improve caching, since each request with a unique set of cookies would need to be regarded as different from other requests and could not be cached (you may receive a different reply from the server based on your cookies, so the cache server cannot serve you the cached response of a previous client). Since lots of services are setting cookies which may be sent to the server (e.g. analytics packages) but have absolutely no influence on the contents of the response, heeding all cookies by default would often completely subvert caching.

Configure the caching server in charge to specifically pay attention to your cookie and to forward it to the origin server. If you have a lot of different cookies, consider giving them a common prefix and whitelist that (e.g. foo-*).