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:
- Use php:
setcookie()
to create a cookie with the two-letter language code.- Example cookie name = value: x_language = es
- 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?