无法在Chrome中访问Cookie,在Firefox中可以正常工作 [英] Cannot access cookies in Chrome, works properly in Firefox
问题描述
基本情况和基本相关信息:
我有一个php代码,在打开< doctype>
标记之前执行。希望(如有必要)在加载其他任何内容之前,根据用户浏览器的语言偏好发送重定向。
脚本尝试根据支持的最高语言偏好来做两件事:
- 使用php:
setcookie()
创建包含两个字母的语言代码的cookie。
- 示例Cookie名称=值:x_language = es
- 使用php:header( Location:。$ requestedSite);重定向到子域,
- 示例域:es.domain.com
示例:
if(isset( $ _COOKIE ['x_language'])){
-根据Cookie值确定正确的子域-
-如果当前不在该子域上,请重定向至该子域-
} else {
setcookie('x_language','es',time()+ 31536000,'/','。domain.com');
header( Location:。$ requestedSite);
}
问题:
Firefox运行正常完美。 Chrome(和其他浏览器)根本无法识别Cookie。
我将其归结为:
-
print_r($ _ COOKIE)
在Firefox中正常工作,并返回一个可爱的填充数组。 -
print_r($ _ COOKIE)
在Chrome中失败,并返回一个空数组。
这是问题的核心,我的功能无法识别Cookie的存在,因为Chrome无法识别。
- 我确保每个浏览器都接受cookie。
- 我已经检查过开发工具,以确保cookie在所有浏览器中都存在(确实如此)。
- 我意识到cookie的价值在下一页加载之前不可用,但这在这里不是问题。
- 在初始setcookie()上方没有输出;
那么我如何让Chrome(和其他浏览器)识别它自己的cookie?有谁知道为什么这一切都可以在Firefox上正常运行,但在其他地方却失败?我创建了一个仅包含以下文件的文件:
<?php
print_r($ _ COOKIE);
?>
再次,我在Firefox中看到了cookie数组。同时,在Chrome,IE,Opera,Safari中,我得到了一个空数组。
OP返回答案:
好的,我将其添加为答案,以防其他任何人遇到这种(完全奇怪的)行为并落在这里:
<事实证明,我的托管服务提供商正在对我不知道的WordPress网站进行一些严重的缓存。
在我发布问题时,我认为使用WordPress并不重要,但显然与之相关。
基本上是这样做的:
使用干净的缓存:
- 访问者1访问该站点。
- PHP将按预期方式处理并产生输出。
-
Visitor 1被提供给php输出(基于浏览器的参数等)。
-
访问者2访问该站点。访客2看到 *访客1的网站版本。
该php被处理一次
这种缓存行为意味着通过php访问cookie根本无法正常工作,但是使用Javascript可以正常工作。
p>(重要说明::事实证明,对于登录时查看站点的任何用户,上述缓存行为均被禁用wordpress ,这是WordPress Cache插件的常见行为,这就是为什么我在Firefox中看到的行为与在其他浏览器中看到的行为不同的原因,因为我是积极使用Firefox登录的。这可能是有用的信息
我的解决方案:
使用Javascript对.php文件运行AJAX查询,该文件将处理访问者的语言首选项并以2字符形式返回输出搜寻者代码(即'en''es''pt''de'等)。
使用AJAX调用php允许我使用php的服务器端访问权限浏览器的语言偏好,同时规避主机的超级农业缓存。
我希望这对某人有帮助!感谢所有试图帮助我解决这个问题的人。
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?
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.
这篇关于无法在Chrome中访问Cookie,在Firefox中可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!