会话变量未设置,但仅在 Internet Explorer 中设置,并非在所有计算机上设置
登录站点时,我正在本地计算机上按预期运行功能,但在远程服务器上失败,但仅限于 Internet Explorer。最棒的是它可以在本地 IE 中运行,但不能在远程计算机上运行。
到底是什么原因会导致这种情况呢?我已单步执行远程计算机上的代码,可以看到在数据库中检查输入的登录值,找到它们,然后调用登录函数。这会设置两个 $_SESSION 变量并重定向到主管理页面。但是,仅在 IE 中(而不是在本地计算机上运行时......这是关键),当您进入主管理页面时,$_SESSION 变量不存在。 var_dump($_SESSION)
给出了我在本地环境中运行此程序时对每个浏览器的期望以及在远程服务器上运行时除了 IE 6、7 和 8 之外的每个浏览器中的期望(在其中我得到null 值,就好像没有为 $_SESSION 设置任何内容)。
这真的让我很困惑所以任何建议都很感激。
例如...在 IE 中,本地运行,var_dump 给我:
array
'Username' => string 'theusername' length=11
'UserID' => string 'somevalue' length=9
在远程服务器上运行(仅限 IE...在其他浏览器中工作正常) var_dump 给我:
array(0){}
代码(一个最小的示例...虽然实际上是一个代码示例此问题不需要):
$User = GetUser($Username, $Password);
if ($User->UserID <> "") { // this works so we call Login()...
Login($User); // this also works and gives expected results. on to redirect...
header("Location: index.php"); // a var_dump at index.php shows that there is no session data at all in IE, remotely.
} else {
header("Location: login.php");
}
function Login($data) {
$_SESSION['Username'] = $data->Username;
$_SESSION['UserID'] = $data->UserID;
// a var dump here gives the expected data in every browser
}
编辑:解决了这个问题。事实是测试服务器上的域名中有一个下划线。不知道为什么,现在也没有时间去 Google 搜索,但下划线(名为 some_client.ourcompany.com 之类的名称)就是问题所在。一定要喜欢 Internet Explorer...它就像一个你根本无法回避的消极好斗的同事。
Logging into a site I'm working on functions as expected on my local machine but fails on the remote server but ONLY in Internet Explorer. The kicker is that it works in IE locally, just not on the remote machine.
What in the world could cause this? I have stepped through the code on the remote machine and can see the entered login values being checked in the database, they are found and then a login function is called. This sets two $_SESSION variables and redirects to the main admin page. However, in IE only (and not when run on local machine... this is key) the $_SESSION variables are not present by the time you get to the main admin page. var_dump($_SESSION)
gives me what I expect on every browser when I am running this in my local environment and in every browser except IE 6, 7 and 8 when run on the remote server (where I get a null value as if nothing has been set for $_SESSION).
This really has me stumped so any advice is appreciated.
For an example... in IE, run locally, var_dump gives me:
array
'Username' => string 'theusername' length=11
'UserID' => string 'somevalue' length=9
Run on the remote server (IE only... works fine in other browsers) var_dump gives me:
array(0){}
Code (a minimal example... though really a code example isn't needed with this issue):
$User = GetUser($Username, $Password);
if ($User->UserID <> "") { // this works so we call Login()...
Login($User); // this also works and gives expected results. on to redirect...
header("Location: index.php"); // a var_dump at index.php shows that there is no session data at all in IE, remotely.
} else {
header("Location: login.php");
}
function Login($data) {
$_SESSION['Username'] = $data->Username;
$_SESSION['UserID'] = $data->UserID;
// a var dump here gives the expected data in every browser
}
EDIT: Solved this. It was the fact that the domain name on the testing server had an underscore in it. No idea why and don't have time to Google for it right now but the underscore, named something like some_client.ourcompany.com, was the problem. Gotta love Internet Explorer... it's like a passive aggressive co-worker that you simply cannot avoid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将其作为答案(由用户 tuzo 建议)放入以便更容易找到。解决了这个问题。事实是测试服务器上的域名中有一个下划线。不知道为什么,现在也没有时间去 Google 搜索,但下划线(名为 some_client.ourcompany.com 之类的名称)就是问题所在。一定要喜欢 Internet Explorer...它就像一个你根本无法回避的消极好斗的同事。
摘自另一个 SO 答案...准确解释了发生了什么:
子域之一是否使用下划线? IE 在接受来自不遵循 URI RFC 的子域的 cookie 时遇到问题。 (http://www.ietf.org/rfc/rfc2396.txt)
Putting this in as an answer (suggested by user tuzo) to make it easier to find. Solved this. It was the fact that the domain name on the testing server had an underscore in it. No idea why and don't have time to Google for it right now but the underscore, named something like some_client.ourcompany.com, was the problem. Gotta love Internet Explorer... it's like a passive aggressive co-worker that you simply cannot avoid.
Taken from another SO answer... explains exactly what's going on:
Does one of the subdomains use an underscore ? IE has problems accepting cookies from subdomain's that dont follow the URI RFC. (http://www.ietf.org/rfc/rfc2396.txt)
检查 php.ini 文件中 session.cookie_domain 的值。如果设置了该值,请确保它符合您的预期。这是我能想到的最重要的事情,它在本地和远程的行为可能有所不同。
另外,如果您将值设置为 session.cookie_lifetime,请尝试在 php.ini 中注释掉该行。当我在 IE 中删除一个值时,我看到了一些奇怪的行为。
这是一个快速会话检查器。如果您重新加载页面并不断看到令牌的新值,则您不会获得持久会话。那么你绝对可以责怪服务器/PHP 配置而不是你的代码。
Check the value of session.cookie_domain in your php.ini file. If that value is set, make sure it's what you'd expect. That's the biggest thing I can think of which could behave differently locally vs remote.
Also, if you have a value set to session.cookie_lifetime, try commenting that line out in your php.ini. I saw some peculiar behavior with IE when I dropped a value in there.
Here's a quickie session checker. If you reload the page and keep seeing new values for the token, you're not getting a persistent session. Then you can definitely blame the server/PHP config and not your code.
“事实上,测试服务器上的域名中有一个下划线”
这是绝对正确的。花了3个多小时解决同样的问题才发现这个帖子。非常感谢高山88
"It was the fact that the domain name on the testing server had an underscore in it"
This is absolutely true. This post was found after spending more than 3 hours to sort out the same problem. Many thanks to gaoshan88
尝试放置 session_start();在您正在进行会话的页面顶部。
并检查您是否以正确的方式分配会话变量:
Try putting session_start(); at the top of the page you are making the sessions in.
and check if you assign the session vars the right way:
我们遇到了同样的问题,但问题不是服务器名称字符,而是服务器时间不正确。这导致我们的会话 cookie 在 IE 中立即失效,而它在其他浏览器上工作。
We've had the same issue, however it was not the server name characters but the server time that wasn't right. This caused our session cookies to be invalid instantly in IE while it worked on other browsers.