php 会话数据仅在 Chrome 中丢失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8247842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:16:25  来源:igfitidea点击:

Session data lost in Chrome only

phpsessiongoogle-chrome

提问by Mathieu Dumoulin

I have a problem similar if not identical to the problem in this thread: Randomly Losing Session Variables Only In Google Chrome & URL Rewriting

我有一个与此线程中的问题类似(如果不完全相同)的问题: 仅在 Google Chrome 和 URL 重写中随机丢失会话变量

But all solutions in that thread don't work for me. I'm getting a strange behavior from only Google Chrome in my PHP/MySQL App. If I try it with Firefox, it works, but Chrome doesn't.

但是该线程中的所有解决方案都不适用于我。我的 PHP/MySQL 应用程序中只有 Google Chrome 出现了奇怪的行为。如果我用 Firefox 尝试它,它会起作用,但 Chrome 不会。

I navigate to some place in my shopping cart and at several places in the code I'll store session data. Don't worry about me starting the session or anything related to that, I've got 11 years in webapp dev, all is done fine.

我导航到购物车中的某个位置,并在代码中的多个位置存储会话数据。不要担心我开始会话或与此相关的任何事情,我在 webapp 开发中有 11 年的经验,一切都很好。

In all browsers, I can var_dump($_SESSION)and get my data back, but in Chrome it doesn't keep the data. Also note that the session does get passed on, I can look in the network monitor and I see the cookie being sent and many other things related to session work but that one $_SESSION['last_viewed_element']is not kept. I also can't seem to set anything else, all gets lost.

在所有浏览器中,我都可以var_dump($_SESSION)取回我的数据,但在 Chrome 中它不保留数据。另请注意,会话确实被传递了,我可以查看网络监视器,我看到正在发送的 cookie 以及与会话工作相关的许多其他内容,但$_SESSION['last_viewed_element']没有保留。我似乎也无法设置任何其他东西,一切都会丢失。

EDIT:

编辑:

Problem resolved by switching from SESSIONS TO COOKIES...

从 SESSIONS 切换到 COOKIES 解决了问题...

采纳答案by Mathieu Dumoulin

After all, no answer, problem still exists, i just made a switch to using cookies instead, if anyone ever gets that problem with chrome+wordpress at the same time, dont lose more time, switch to cookies...

毕竟,没有答案,问题仍然存在,我只是改用 cookie,如果有人同时遇到 chrome+wordpress 的问题,不要浪费更多时间,切换到 cookie...

回答by Ben

I had a very similar problem, in my case the problem was a 404called due to a missing favicon.ico in Chromeonly. The 404.php called the footer which altered the SessionVariables. I hope that helps someone.

我有一个非常相似的问题,在我的情况下,问题是由于仅在Chrome 中缺少 favicon.ico 导致的404调用。404.php 调用了改变会话变量的页脚。我希望能帮助某人。

回答by user1665622

The issue could be your server is looking for favicons, if it is not found the server throws out a 302 redirect, which kills the session variables.

问题可能是您的服务器正在寻找网站图标,如果没有找到,服务器会抛出 302 重定向,这会杀死会话变量。

回答by Camilo

I had this issue and was able to fix it. Chrome keeps looking for a .ico file and for some reason it was affecting it. Once I placed the .ico file in the root of the site everything started working. Crazy but true.

我遇到了这个问题并且能够修复它。Chrome 一直在寻找 .ico 文件,但出于某种原因,它正在影响它。一旦我将 .ico 文件放在网站的根目录中,一切都开始工作了。疯狂但真实。

回答by Kash

I faced same problem, but on IIS with ASP.Net MVC. IE and Firefox were doing fine, but on Chrome I was losing session data. Eventually found out that a 404 error was clearing a cookie in Chrome. Below are the steps I followed to find the problem and resolve. I suggest others to try:

我遇到了同样的问题,但是在带有 ASP.Net MVC 的 IIS 上。IE 和 Firefox 运行良好,但在 Chrome 上我丢失了会话数据。最终发现 404 错误正在清除 Chrome 中的 cookie。以下是我发现问题并解决的步骤。我建议其他人尝试:

  1. On Chrome, Use Tools -> Developer Tools. Refresh the page so "Developer Tools" starts showing data.

  2. On Developer tools, Check Resources -> Cookies. Right after a successful log in, I had 2 cookies for the domain I was testing. On navigating to the page where I lost session, one of the cookies did not show up anymore. The screenshot was taken after the fix, showing both cookies: enter image description here

  3. Now check Network tab. Look carefully for any resource (html/image/css/js/...) which has any error. I had a 404 error for a font file. The 404 error was caused by missing mime type in IIS. fixing the 404 error cleared the problem in Chrome. The screenshot, again taken after fix, had all resources with OK status: enter image description here

  1. 在 Chrome 上,使用工具 -> 开发人员工具。刷新页面,以便“开发人员工具”开始显示数据。

  2. 在开发者工具上,检查资源 -> Cookies。成功登录后,我有 2 个 cookie 用于我正在测试的域。在导航到我丢失会话的页面时,其中一个 cookie 不再显示。屏幕截图是在修复后截取的,显示了两个 cookie: 在此处输入图片说明

  3. 现在检查网络选项卡。仔细查看任何有错误的资源 (html/image/css/js/...)。我有一个字体文件的 404 错误。404 错误是由 IIS 中缺少 MIME 类型引起的。修复 404 错误清除了 Chrome 中的问题。修复后再次截取的屏幕截图,所有资源都处于 OK 状态:在此处输入图片说明

The bonus was, investigating this problem helped me find out missing mime type in IIS, which was affecting more pages on all browsers.

好处是,调查这个问题帮助我找到了 IIS 中缺少的 MIME 类型,这影响了所有浏览器上的更多页面。

回答by user2246924

Had same problem and finally solved. Login set session with domain.com but in the redirect it was www.domain.com. IE and FF seem to assume www and no www are same but Chrome doesn't. Found by checking Host in network log for each page load.

有同样的问题,终于解决了。登录设置与 domain.com 的会话,但在重定向中它是 www.domain.com。IE 和 FF 似乎假设 www 和没有 www 是相同的,但 Chrome 没有。通过检查网络日志中的主机为每个页面加载找到。

回答by sijo vijayan

Just try this before wasting your time

在浪费你的时间之前试试这个

If you are already logged in your webspace ( control panel / Cpanel / Plesk Panel) in the same browser. Then logout from that control panel and clear the cookies and try Again

如果您已经在同一浏览器中登录您的网站空间(控制面板/Cpanel/ Plesk 面板)。然后从该控制面板注销并清除 cookie 并再次尝试

In case of

的情况下

session data lost in chrome only

会话数据仅在 chrome 中丢失

In my case I just reset chrome browser

就我而言,我只是重置了 chrome 浏览器

Go to chrome://settings/ then click advanced then reset

转到 chrome://settings/ 然后单击高级然后重置

enter image description here

在此处输入图片说明

回答by Matt

HA! i finally solved it!

哈!我终于解决了!

When doing a header()redirect in PHP, you must do a die()right after it. THAT only solves it for all browsers except for Chrome.

header()在 PHP 中进行重定向时,您必须die()在它之后立即执行。这只能解决除 Chrome 之外的所有浏览器的问题。

For Chrome you also gotta do a session_write_close()right before the header()

对于 Chrome,您还必须session_write_close()header()

Sweeeeeeeeet success

甜蜜成功

回答by rota

The code I was working with had the same issue. Solved by removing the following:

我正在使用的代码有同样的问题。通过删除以下内容解决:

session_id($_GET['sid']);
session_write_close();

回答by kornstar11

Looking on the following link: http://code.google.com/p/chromium/issues/detail?id=45582

查看以下链接:http: //code.google.com/p/chromium/issues/detail?id=45582

I belive the issue is with PHP getting the request that did not match a file and then not properly handling 404's correctly. I had to tell Nginx to match any URL with favicon.ico and then return a 404.

我相信问题在于 PHP 收到与文件不匹配的请求,然后没有正确处理 404。我不得不告诉 Nginx 将任何 URL 与 favicon.ico 匹配,然后返回 404。

Here is my line for Nginx:

这是我的 Nginx 线路:

 if ($request_uri ~ 'favicon') {
            return 404;
 }

回答by Rafael Alves

I solved the problem by removing the line:

我通过删除该行解决了这个问题:

base href="http://mysite/"

from the headtag in the HTML code.

来自headHTML 代码中的标记。