0

I am trying to check if a cookie is set before loading some scripts, but when I use $_SERVER['HTTP_COOKIE'] or $_COOKIE['cookie_name'] to check for the cookie, it seems to get ignored in Firefox and Safari even though I am not blocking cookies or in a private window or anything like that.

I have tried echoing all of the cookie names with the following:

if (isset($_SERVER['HTTP_COOKIE'])) {
  $cookies = explode('; ', $_SERVER['HTTP_COOKIE']);
  foreach ($cookies as $cookie ) {
    echo '<pre>cookie: ' , $cookie , '</pre>';
  }
}

and in Chrome it lists all of the cookies, but in Firefox and Safari there is nothing. I have also tried checking the value of $COOKIE like this:

if(count($_COOKIE) > 0) {
      echo "Cookies are enabled.";
    } else {
      echo "Cookies are disabled.";
    }

and in Chrome I get Cookies are enabled, but in Firefox/Safari I get Cookies are disabled. I am not seeing any errors related to the cookies, but I guess it is worth noting that I am working on a testing server and I do have some warnings about cross-site requests in relation to Google Analytics (e.g. Referrer Policy: Ignoring the less restricted referrer policy “origin-when-cross-origin” for the cross-site request: https://www.googletagmanager.com/gtag/js?id=...).

Does anyone have any ideas on how to proceed with debugging?

Thanks!

hmoore
  • 155
  • 1
  • 3
  • 13
  • 1
    If you use Developer Tools to view the cookies for the site, do you see the expected cookies? – Barmar Jun 06 '23 at 21:06
  • Please check the accepted answer to [THIS QUESTION](https://stackoverflow.com/questions/1211822/php-cookies-problem-works-in-firefox-not-in-other-browser) as it might be helpful. Report back. – Zak Jun 06 '23 at 21:11
  • Also we don't know if you are actually using a domain or something like `localhost` -- [THIS QUESTION](https://stackoverflow.com/questions/489369/can-i-use-localhost-as-the-domain-when-setting-an-http-cookie) might also be helpful. – Zak Jun 06 '23 at 21:14
  • @Barmar, yes I am seeing the cookies in the storage section of Firefox and Safari developer tools. – hmoore Jun 07 '23 at 13:05
  • @Zak, I am using a domain, not localhost. On localhost it works in Firefox, but in Safari the cookies aren't being set. I will check out the answer you linked. – hmoore Jun 07 '23 at 13:05
  • Just to follow up, I am able to set the cookies in Safari on localhost if I remove the ```secure``` attribute (since localhost is not HTTPS). However, I still can't read the cookies on my testing server even though I can see that they have been set. – hmoore Jun 07 '23 at 15:01

0 Answers0