PHP 会话不适用于 IE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/306132/
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-24 22:18:47  来源:igfitidea点击:

PHP session doesn't work with IE

phpinternet-explorersession

提问by seans

I have a site made with php which uses server side sessions throughout the site.
In fact, it's a site with a user login which depends on session variables and if there were a problem with allsession variables, no pages would load at all.

我有一个用 php 制作的网站,它在整个网站上使用服务器端会话。
事实上,这是一个用户登录的站点,它依赖于会话变量,如果所有会话变量都存在问题,则根本不会加载任何页面。

On the site, there's an iframe that holds a feed of little messages from other users.
Those little messages have clickable photos next to them that open the user's profile.
Now, each page requires some formatting to open the user's profile on that specific page...there's really only a few problempages, but those pages have to have the onclick functions formatted a little differently or they break the page.
So I set a session variable on each page ($_SESSION["current_page"]) that lets the feed know how to format the clickable photos. Now Firefox, Opera, Chrome, Safari all work as they are supposed to.
But IE6 and IE7 are having problems on the pages that require special formatting.
So after pulling my hair out a bit, I eventually got around to printing my session variables form the server.
And lo and behold, on the special pages, ($_SESSION["current_page"]) is always set to "main" instead of "special1" or "special2".

在该站点上,有一个 iframe 用于保存来自其他用户的小消息提要。
这些小消息旁边有可点击的照片,可以打开用户的个人资料。
现在,每个页面都需要一些格式才能在该特定页面上打开用户的个人资料……实际上只有几个有问题的页面,但这些页面的 onclick 功能的格式必须稍有不同,否则会破坏页面。
因此,我在每个页面 ( $_SESSION["current_page"])上设置了一个会话变量,让提要知道如何设置可点击照片的格式。现在 Firefox、Opera、Chrome、Safari 都可以正常工作。
但是 IE6 和 IE7 在需要特殊格式的页面上有问题。
因此,在稍微拉开我的头发后,我最终开始从服务器打印我的会话变量。
瞧,在特殊页面上, ( $_SESSION["current_page"]) 始终设置为“main”而不是“special1”或“special2”。

I printed the same session variable in Firefox and all the other browsers I mentioned and they print out "special1" or "special2" as they're supposed to.
Can anyone think of something - possibly related to the fact that the feed is in an iframe??? - that would cause IE to treat server side session variables differently or somehow launch page "main" silently in the background?
I have checked the feed very carefully for any reference to page "main" - it doesn't seem like there's any ways it's loading that page.

我在 Firefox 和我提到的所有其他浏览器中打印了相同的会话变量,它们按预期打印出“special1”或“special2”。
任何人都可以想到一些东西 - 可能与提要在 iframe 中的事实有关???- 这会导致 IE 以不同的方式处理服务器端会话变量,或者以某种方式在后台静默启动页面“主”?
我已经非常仔细地检查了提要是否有任何对页面“主”的引用——它似乎没有任何方式加载该页面。

this doesn't make sense to me.

这对我来说没有意义。

采纳答案by seans

I thought some people might find the solution to this problem interesting. Fiddler certainly helped here. Thanks to Fiddler, I could see that I was, in fact, hitting the page main.php (thus setting the session variable moments after setting it on the target page), but the server was defaulting there after getting a 302 on the root of the site. This was all happening silently in the background, and beforemy onload="" javascript ran.

我想有些人可能会发现这个问题的解决方案很有趣。Fiddler 在这里肯定有所帮助。感谢 Fiddler,我可以看到我实际上是在点击页面 main.php(因此在目标页面上设置会话变量后立即设置会话变量),但是服务器在获得 302 的根目录后默认在那里网站。这一切都在后台悄悄发生,我的 onload="" javascript 运行之前。

So I was sure something onthose pages was causing an error, but not a catastrophic one.

所以我确信那些页面上的某些东西导致了错误,但不是灾难性的。

here it is: <img src= "" >

这里是: <img src= "" >

IE was freaking out about the blank src attribute and hitting the server root and the defaulting to page main. I don't fully understand the mechanics happening here. I also don't understand if this is how IE is supposed to behave (it is a malformed img tag after all) or not. Is this a bug?

IE 对空白的 src 属性感到害怕,并击中了服务器根目录和默认页面主页面。我不完全理解这里发生的机制。我也不明白这是否是 IE 应该表现的方式(毕竟它是一个格式错误的 img 标签)。这是一个错误吗?

回答by Kieveli

Check the name of the server machine. IE has problems with machine names that contain '-' or '_' - they cannot maintain a session! I've had this problem twice in the past, and it always takes me weeks to figure out, and I'm shocked IE hasn't fixed it.

检查服务器机器的名称。IE 有包含“-”或“_”的机器名称的问题——它们无法维持会话!过去我遇到过两次这个问题,我总是需要几周的时间才能弄清楚,我很震惊 IE 还没有修复它。

Just rename the machine to have no strange characters! You can get it working if you just use the IP address of the server in the url to test.

只需将机器重命名为没有奇怪的字符!如果您仅使用 url 中服务器的 IP 地址进行测试,则可以使其正常工作。

回答by Piskvor left the building

Try testing the page while using some sort of monitoring proxy (I use Fiddler) and see what pages the browser requests. That might give you some clues to what's going on.

尝试在使用某种监视代理(我使用Fiddler)时测试页面并查看浏览器请求的页面。这可能会给你一些关于正在发生的事情的线索。

Also, try capturing the requests/responses from different browsers and see what IE is doing differently (order of requests, content of requests?).

此外,尝试捕获来自不同浏览器的请求/响应,并查看 IE 的不同之处(请求顺序、请求内容?)。

To pinpoint the problem, can you rewrite the code withoutusing SESSION (it's mentioned in one of the other answers)? Maybe IE is accessing the pages in different order than other browsers? Maybe it is requesting the main page more than once, which means that the session var is set to "main"? Without session variables, the pages won't affect each other's state.

为了查明问题,您能否在使用 SESSION 的情况下重写代码(在其他答案之一中提到过)?也许 IE 访问页面的顺序与其他浏览器不同?也许它不止一次请求主页,这意味着会话变量设置为“main”?没有会话变量,页面不会影响彼此的状态。

回答by RAT

In most cases, this php line at file begining will be enough:

在大多数情况下,文件开头的 php 行就足够了:

header('P3P: CP=”NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM”');

If it isn't, for IE7 you may also try:

如果不是,对于 IE7,您也可以尝试:

header('P3P: CP=”NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM”');

header('Set-Cookie: SIDNAME=ronty; path=/; secure');

header('Cache-Control: no-cache');

header('Pragma: no-cache');

And if that doesn't work for IE6, you may use GET params for session ID:

如果这不适用于 IE6,您可以使用 GET 参数作为会话 ID:

header('location: land_for_sale.php?phpSESSID='.session_id());

回答by David Stone

I found if you added header('P3P: CP="CAO PSA OUR"');to the top of your doc. It seems to have fixed the problem.

我发现如果您添加header('P3P: CP="CAO PSA OUR"');到文档的顶部。似乎已经解决了这个问题。

回答by encanodon

I have the same problem and it's SOLVED now.

我有同样的问题,现在已经解决了。

The blank or empty attribute's values of any IMG tags cause the problem. For me, I used JavaScript to change IMG object's source to an empty value. Doing that could also make the problem.

任何 IMG 标签的空白或空属性值都会导致问题。对我来说,我使用 JavaScript 将 IMG 对象的源更改为空值。这样做也可能导致问题。

回答by seb

I had this problem, and it was due to the date on my dev box being out. Firefox didn't mind, IE and chrome were seeing the session as being expired as soon as it was set.

我遇到了这个问题,这是由于我的开发箱上的日期已用完。Firefox 并不介意,IE 和 chrome 会在会话设置后立即将其视为过期。

回答by seb

I had the same problem with ie7 and this is what I do:

我在 ie7 上遇到了同样的问题,这就是我所做的:

If you have this problem using a IIS or Apache in Windows Server, look at the URL where you are redirecting it must be writed in the same way as the URL where you was before the redirection.

如果您在 Windows Server 中使用 IIS 或 Apache 遇到此问题,请查看您重定向的 URL,它必须以与重定向前的 URL 相同的方式编写。

For example: site.com/pages/index.phpredirection to site.com/Pages/index2.phpis going to loose the session in IE7 because the capital letter in Pages.

例如: site.com/ pages/index.php重定向到site.com/ Pages/index2.php将在 IE7 中丢失会话,因为 Pages 中的大写字母。

回答by dotcolor

Maybe it's session.cookie_lifetime. I have faced the same problem. I updated session.cookie_lifetime: 4500to session.cookie_lifetime:0. This means the session cookie never expires until the browser shuts down.

也许是session.cookie_lifetime。我遇到了同样的问题。我更新session.cookie_lifetime: 4500session.cookie_lifetime:0. 这意味着会话 cookie 永远不会过期,直到浏览器关闭。