重定向后丢失会话变量 [英] Losing session variables after redirect

查看:55
本文介绍了重定向后丢失会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 用户填写用户名和密码.
  2. 如果正确,页面会将诸如 user_id 之类的信息加载到会话变量中.
  3. 该脚本进行了 header('Location') 重定向.
  4. 不知何故,下一页无法识别会话......怎么会这样?
  1. User fills in username and password.
  2. If it's correct, the page loads some information such as user_id to a session variable.
  3. The script makes a header('Location') redirect.
  4. Somehow the next page doesn't recognize the session... how come?

重定向到同一个域,所有页面都有session_start();

The redirection is to the same domain, and all pages have session_start();

而且我发现它在 IE 中比在 FF 中更可能发生......很奇怪.

And I found it more likely to happen in IE than in FF... strange.

推荐答案

是否有可能没有启用 cookie?

Is it possible that cookies aren't enabled?

为了能够将会话变量与特定的客户端实例相关联(即如何同时在您的浏览器和我的浏览器上使用会话变量而不会发生冲突),会话 ID"(或SID")是每个会话生成的.该 ID 通常以 cookie 的形式存储在服务器和客户端上.但是,如果未启用 cookie,则会话 ID 作为每个请求中 URL 的查询字符串的一部分传递,以便服务器可以知道哪个会话 ID 属于客户端.

In order to be able to associate session variables with a specific client instance (ie. how session variables can be used on your browser and my browser at the same time without getting into a conflict), a "session ID" (or "SID") is generated per session. This ID is stored on the server, as well as on the client, usually in the form of a cookie. However, if cookies are not enabled, the session ID is passed along as part of the query string of the URL in each request so that the server can know what session ID belongs to the client.

当您通过header() 调用重定向时,PHP 不会自动将 SID 插入到新请求中,因此您需要自己附加它,格式为:

When you redirect by a header() call, PHP does not automatically insert the SID into the new request, so you will need to append it yourself, in the form of:

header("Location: my_url.com/my_page.php?" . SID)

其中 SID 是一个常量由 PHP 定义,包含查询字符串的必要部分(等效于 session_name() .'=' . session_id(),如果存在会话 ID).

where SID is a constant defined by PHP that contains the necessary part of the query string (equivalent to session_name() . '=' . session_id(), if a session ID exists).

有关详细信息,请参阅传递会话 ID.

See Passing the Session ID for more details.

这篇关于重定向后丢失会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆