Skip to content
Advertisement

Cookie values not being returned in Firefox

This works totally fine on my local dev version in all browsers…

foreach ($_COOKIE as $key=>$val) {
    echo $val . '<br />';       
}

…and will return all the current cookie values on the page, like this:

10000
20000
30000

The problem is that it doesn’t return anything in Firefox or Edge on production. It does work in Chrome on production though.

Here’s how Chrome dev tools sees the cookies on my local version:

Name                Value   Domain          Path    Expires / Max-Age           Size    Priority
------------------------------------------------------------------------------------------------
wish_product_10425  10000   testing.local   /       2021-08-08T04:48:37.000Z    23      Medium  
wish_product_10499  20000   testing.local   /       2021-08-09T01:54:38.000Z    23      Medium  
wish_product_10644  30000   testing.local   /       2021-08-09T01:54:39.000Z    23      Medium  

…and here’s how Firefox dev tools sees the cookies on my local version:

Name                Value   Domain          Path    Expires / Max-Age           Size    HttpOnly    Secure  Priority    SameSite
--------------------------------------------------------------------------------------------------------------------------------
wish_product_10425  10000   testing.local   /       2021-08-08T04:48:37.000Z    23      false       false       Medium  None
wish_product_10499  20000   testing.local   /       2021-08-09T01:54:38.000Z    23      false       false       Medium  None
wish_product_10644  30000   testing.local   /       2021-08-09T01:54:39.000Z    23      false       false       Medium  None

In summary, it works on my local dev version in all browsers, but on production it only works in Chrome.

Just FYI yes on production the cookie domain is the correct production domain. There are no PHP errors either.

Advertisement

Answer

After fixing the cookies based on excellent suggestions by @CBroe, this loop still did not work:

foreach ($_COOKIE as $key=>$val) {
   echo $val . '<br />';       
}

…until I tried it on a completely different web host, where it worked fine (and so did my other cookie related scripts).

The problem turned out to be our host WPEngine who, according to the support conversation I had, says the only way to get around the issue was for them to exclude the desired cookie name (or partial cookie name) from their cache.

Once that was done, everything worked.

More info: https://wpengine.com/support/cookies-and-php-sessions/

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement