Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access cookies in Chrome, works properly in Firefox

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

The script attempts to do two things based on highest supported language preference:

  1. Use php: setcookie() to create a cookie with the two-letter language code.
    • Example cookie name = value: x_language = es
  2. Use php: header("Location: " . $requestedSite); to redirect to a subdomain,
    • Example domain: 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);
}

The problem: Firefox works perfectly. Chrome (and other browsers) fail to recognize the cookies at all.

I've boiled it down to this:

  • print_r($_COOKIE) works properly in Firefox, and returns a lovely, populated array.
  • print_r($_COOKIE) fails in Chrome, and returns an empty array.

This is the core of the problem, my function doesn't recognize the existence of a cookie because Chrome doesn't.

  • I've made sure every browser accepts cookies.
  • I've checked dev tools to make sure the cookie is in place on all browsers, (it is).
  • I realize a cookie's value isn't available until the next page load, but that isn't an issue here. Even after it is set, it won't read.
  • There is no output above the initial setcookie();

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


On a lark I decided to try this. I created a file that only contains:

<?php
print_r($_COOKIE);
?>

Again, I see the cookie array in Firefox. Meanwhile, in Chrome, IE, Opera, Safari, I get an empty array. Could this be a server issue?

like image 794
FourStair Avatar asked Mar 08 '13 19:03

FourStair


People also ask

Is Firefox blocking cookies?

Note: Firefox also includes Total Cookie Protection, which creates a “cookie jar” for every website. This feature keeps cookies in the site where they were created so that they can't track you across websites.

How do I get Firefox to accept cookies?

Click Tools > Options. Click Privacy in the top panel. Click the Cookies tab. Select the checkbox labeled 'Allow sites to set cookies.

How do you fix your web browser is not configured to accept session cookies Please enable cookies in your web browser and try again?

To enable cookies in Google Chrome (Android):At the top right, tap More More and then Settings. Tap Site settings and then Cookies. Next to “Cookies,” switch the setting on. To allow third-party cookies, check the box next to “Allow third-party cookies.”


1 Answers

OP returns with answer:

Alright, I'm adding this as an 'Answer' in case anyone else comes across this (totally bizarre) behavior and lands here:

It turns out my hosting provider was doing some seriously aggressive caching with my WordPress site that I was unaware of.

At the time I posted my question, I didn't think being on WordPress was relevant, but apparently it was.

Basically it was doing this:


With a clean Cache:

  • Visitor 1 visits the site.
  • The php processes and produces output as expected.
  • Visitor 1 is served php output (based on his browser's parameters and such).

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

The php is processed once and only once per Cache-clear.

This caching behavior meant that accessing cookies through php was simply not going to work right, but accessing them with Javascript WOULD work.

(Important note: It turns out the above-stated caching behavior is disabled for any user viewing the site while logged into wordpress, and this is common behavior for WordPress Cache plugins. That is why I was seeing different behavior in Firefox than I saw in other browsers, because I was actively logged in with Firefox. This could be a helpful piece of information for someone out there.)


My solution:

Use Javascript to run an AJAX query to a .php file which would process the language preferences of the visitor and return the output as a 2-character code, (i.e. 'en' 'es' 'pt' 'de', etc).

Using AJAX to call php allowed me to use php's server-side access to a browser's language preferences while circumventing the super-agro caching of my host.

I hope this helps someone! And thanks to everyone who tried to help me out with this.

like image 94
FourStair Avatar answered Oct 04 '22 20:10

FourStair