PHP 会话在 PHP5 中不起作用

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

PHP Session not working in PHP5

phpsession

提问by Martijn

I have 2 pages: login.php and index.php. Both pages start with

我有 2 个页面:login.php 和 index.php。两个页面都以

session_start();

When I set

当我设置

$_SESSION['user'] = "name"; 

in login.php and than open index.php, my session object is empty. How come?

在 login.php 而不是打开 index.php,我的会话对象是空的。怎么来的?

EDIT:

编辑:

I found the problem: IE 7. I had to grand access to my domain. However, I thought a session is stored on the server, instead of the client? Than why do I have IE grand access to my domain? (http://www.pcwindowstips.com/2007/09/04/how-to-enable-cookies-in-internet-explorer-7/)

我发现了这个问题:IE 7。我必须对我的域进行盛大的访问。但是,我认为会话存储在服务器上,而不是客户端上?为什么我可以 IE 访问我的域?( http://www.pcwindowstips.com/2007/09/04/how-to-enable-cookies-in-internet-explorer-7/)

回答by Kip

I thought a session is stored on the server, instead of the client? Than why do I have IE grant access to my domain? (http://www.pcwindowstips.com/2007/09/04/how-to-enable-cookies-in-internet-explorer-7/)

我认为会话存储在服务器上,而不是客户端上?为什么我有 IE 授予访问我的域的权限?( http://www.pcwindowstips.com/2007/09/04/how-to-enable-cookies-in-internet-explorer-7/)

The way sessions work is that a session cookie is stored for the site, which contains your session ID. The only way the server knows who you are is when it reads the session ID cookie on every page load. All of the $_SESSION data is stored on the server for each user, but the cookie must be set for the server to know which $_SESSION data to retrieve.

会话的工作方式是为站点存储一个会话 cookie,其中包含您的会话 ID。服务器知道您是谁的唯一方法是在每次加载页面时读取会话 ID cookie。所有 $_SESSION 数据都存储在每个用户的服务器上,但必须设置 cookie 以便服务器知道要检索哪些 $_SESSION 数据。

This is also why you can essentially "become" another user if you obtain their session id cookie.

这也是为什么如果您获得他们的会话 ID cookie,您基本上可以“成为”另一个用户。

回答by Gumbo

Internet Explorers have a stricter cookie policy than most other browsers. Check your session cookie parameters(see also session_get_cookie_params()) and try to replace the default values by explicit values where possible. Additionally you might send a [fake P3P policy](http://msdn.microsoft.com/en-us/library/ms537343(VS.85).aspx)to satisfy the Internet Explorers.

Internet Explorer 拥有比大多数其他浏览器更严格的 cookie 政策。检查您的会话 cookie 参数(另请参阅 参考资料session_get_cookie_params()),并在可能的情况下尝试用显式值替换默认值。此外,您可能会发送 [假 P3P 策略]( http://msdn.microsoft.com/en-us/library/ms537343(VS.85).aspx)以满足 Internet Explorer。

回答by apolo

Perhaps this variable in php.ini is mapping to an existing path

也许 php.ini 中的这个变量映射到现有路径

session.save_path = "c:/wrong/path"

session.save_path = "c:/wrong/path"

回答by brad tittle

Here is something that happened to me that might shed light for someone. My session wasn't working properly. IE 8 and Firefox were losing the session information.

这是发生在我身上的事情,可能会为某些人带来启发。我的会话工作不正常。IE 8 和 Firefox 丢失了会话信息。

I included a file. That included file had an extra carriage return after the trailing &ques?>

我包含了一个文件。包含的文件在尾随 &ques?> 之后有一个额外的回车符?

That carriage return started the session. I put session_start after the include. BOOM.

那个回车开始了会话。我把 session_start 放在包含之后。繁荣。

回答by Kip

Not much info here, I'll try to use my psychic powers.

这里的信息不多,我会尝试使用我的精神力量。

After the user logs in, do you set the session var and then redirect the user to index.php using an http header? If so, I don't think the session cookie gets sent to the user. If that is the case, the solutions are:

用户登录后,是否设置会话变量,然后使用 http 标头将用户重定向到 index.php?如果是这样,我认为会话 cookie 不会发送给用户。如果是这种情况,解决方法是:

  1. call session_start() when the login form is initially displayed (not just after the user posts back to it); or:
  2. display a "login successful!" message and then redirect with a meta-refresh, or just provide a link to index.php.
  1. 在最初显示登录表单时调用 session_start()(不仅仅是在用户回帖之后);或者:
  2. 显示“登录成功!” 消息,然后使用元刷新重定向,或者只提供一个指向 index.php 的链接。


You can also try to dump the session ID on both pages, to see if you are somehow starting a new session:

您还可以尝试在两个页面上转储会话 ID,以查看您是否以某种方式开始新会话:

echo 'Session ID is: ' . SID . "<br/>\n"

回答by Charles Alves

You need verify if the cookies are enabled and nothing ( this includes blank lines in the beginning or in the end of archive) sent to browser before you call session_start().

在调用 session_start() 之前,您需要验证 cookie 是否已启用,并且没有任何内容(包括存档开头或结尾的空白行)发送到浏览器。