Cannot access cookies in Chrome, works correctly in Firefox.

Main situation and basic information: I have php code that runs before the opening <doctype> . The hope was to (if necessary) send a redirect based on the user's browser language preferences before anything loads.

The script tries to do two things based on the maximum supported language preference:

  • Use php: setcookie() to create a cookie with a two-letter language code.
    • Example cookie name = value: x_language = es
  • Use php: header ("Location:". $ AskSite); to redirect to a subdomain,
    • Domain example: es.domain.com

Example:

 if (isset($_COOKIE['x_language'])) { -Determine correct subdomain based on cookie value- -If not currently on that subdomain, redirect to it- } else { setcookie('x_language','es',time() + 31536000 ,'/','.domain.com' ); header("Location: " . $requestedSite); } 

Problem: Firefox is working fine. Chrome (and other browsers) do not recognize cookies at all.

I digested this:

  • print_r($_COOKIE) works correctly in Firefox and returns a beautiful, populated array.
  • print_r($_COOKIE) does not work in Chrome and returns an empty array.

This is the core of the problem , my function does not recognize the presence of a cookie, because Chrome does not.

  • I made sure that every browser accepts cookies.
  • I checked the dev tools to make sure the cookie is on all browsers (this is the case).
  • I understand that the cookie value is not available until the next page loads, but this is not a problem. Even after it is installed, it will not read.
  • At the initial stage, setcookie () has no way out.

So, how do I get Chrome (and other browsers) to recognize my own cookies ?! Does anyone know why all this will work flawlessly in Firefox, but fail elsewhere?


In the early morning I decided to try this. I created a file that contains only:

 <?php print_r($_COOKIE); ?> 

Again, I see an array of cookies in Firefox. Meanwhile, in Chrome, IE, Opera, Safari, I get an empty array. Could this be a problem with the server?

+4
source share
2 answers

The OP returns with the answer:

Well, I add this as the “Answer” if someone else comes across this (completely bizarre) behavior and lands here:

It turns out that my hosting provider was seriously aggressive caching from my WordPress site, which I did not know about.

At the time I posted my question, I didn’t think that being on WordPress mattered, but apparently it was.

This was mainly done as follows:


With pure cache

  • Visitor 1 visits the site.
  • php processes and displays the result as expected.
  • Visitor 1 serves the php output (based on its browser settings, etc.).

  • Visitor 2 visits the site. Visitor 2 sees the * version of visitor 1 of the site.

php is processed once and only once for Cache-clear.

This caching behavior meant that accessing cookies through php simply would not work correctly, but accessing them with Javascript would work.

( Important Note: It turned out that the above caching behavior is disabled for any user viewing the site when logging into Wordpress, and this is the usual behavior for WordPress Cache plugins. That's why I saw different behavior in Firefox than in other browsers, because I am actively logged in with Firefox. This might be useful information for someone out there.)


My decision:

Use Javascript to run an AJAX request into a .php file that processes the visitor’s language preferences and returns the result as a 2-character code (e.g. 'en' 'es' 'pt' 'de', etc.).

Using AJAX to call php allowed me to use php server access to the browser language settings, bypassing the super-agro caching of my host.

Hope this helps someone! And thanks to everyone who tried to help me with this.

+1
source

I did not have this problem with the code below. I managed to go to example.com and immediately redirect to en.example.com and see the cookie in $_COOKIES . If I used en.example.com?set=fr , I would be redirected to fr.example.com every time I tried example.com . Hope this is what you were looking for!

 <?php print_r($_COOKIE); if(isset($_GET['nuke'])) { setcookie('x_language','',time()-1000,'/','.example.com'); echo 'It has been nuked!'; exit; } else if(isset($_GET['set'])) { setcookie('x_language',$_GET['set'],time() + 31536000, '/','.example.com'); $_COOKIE['x_language'] = $_GET['set']; } if (isset($_COOKIE['x_language'])) { $redirect = $_COOKIE['x_language'].'.example.com'; if($_SERVER['HTTP_HOST'] != $redirect) header('Location: http://'.$redirect); } else { setcookie('x_language','en',time() + 31536000,'/','.example.com'); $redirect = 'http://en.example.com'; header('Location: '.$redirect); } echo '<br />Cookie: '.$_COOKIE['x_language'].' Domain: '.$_SERVER["HTTP_HOST"]; ?> 
0
source