简体   繁体   English

重定向后丢失会话变量

[英]Losing session variables after redirect

  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. 如果正确,页面会将一些信息(如user_id加载到会话变量中。
  3. The script makes a header('Location') redirect. 该脚本使header('Location')重定向。
  4. Somehow the next page doesn't recognize the session... how come? 不知怎的,下一页不承认会议......怎么来的?

The redirection is to the same domain, and all pages have session_start(); 重定向到同一个域,所有页面都有session_start();

And I found it more likely to happen in IE than in FF... strange. 而且我发现它更有可能发生在IE中而不是FF ...奇怪。

Is it possible that cookies aren't enabled? 是否可能未启用Cookie?

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. 为了能够将会话变量与特定客户端实例相关联(即,如何在浏览器和我的浏览器上同时使用会话变量而不会发生冲突),可以使用“会话ID”(或“SID”) )每个会话生成。 This ID is stored on the server, as well as on the client, usually in the form of a cookie. 此ID通常以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. 但是,如果未启用cookie,则会话ID将作为每个请求中URL的查询字符串的一部分传递,以便服务器可以知道哪个会话ID属于客户端。

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()调用重定向时,PHP不会自动将SID插入到新请求中,因此您需要自己附加它,格式为:

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

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). 其中SID是由PHP定义的常量,包含查询字符串的必要部分(相当于session_name() . '=' . session_id() ,如果存在会话ID)。

See Passing the Session ID for more details. 有关更多详细信息,请参阅传递会话ID

I just had a similar issue, the solution was to simply add an exit(); 我刚才遇到了类似的问题,解决方法是简单地添加一个exit(); instruction under the header(..) redirection. 标题(..)重定向下的指令。

Two thoughts: 两个想法:

  1. Is session_start() located at the top of the scripts, before anything is sent to the browser? 在将任何内容发送到浏览器之前,session_start()是否位于脚本的顶部?
  2. Is the domain exactly the same? 域名是否完全相同? www.mydomain.com re-directing to mydomain.com would lead to the problem you describe. www.mydomain.com重定向到mydomain.com会导致你描述的问题。
header("Location: my_url.com/my_page.php?" . SID)
exit();

It only worked after I added exit() below the header(); 它只在我在header();下面添加exit()后才起作用header();

The WordPress documentation states that cookies will be cleared if the user's password is changed. WordPress文档指出,如果更改用户密码,将清除cookie。 That will kill the session, regardless of whether a redirect happens. 无论重定向是否发生,这都会导致会话中断。 So long as you can prevent the cookies from being cleared (and an exit() may do that, as suggested in other answers) than the session should remain. 只要您可以阻止cookie被清除(并且exit()可以执行该操作,如其他答案所示),而不是会话应该保留。

Note: If current user's password is being updated, then the cookies will be cleared! 注意:如果正在更新当前用户的密码,则cookie将被清除!

http://codex.wordpress.org/Function_Reference/wp_update_user http://codex.wordpress.org/Function_Reference/wp_update_user

I had this problem today and have been searching for a way to fix it. 我今天遇到了这个问题,一直在寻找解决方法。 I already had what everyone else has mentioned and could not find an answer anywhere. 我已经拥有了其他所有人都提到的内容,无法在任何地方找到答案。

Eventually I found the answer after watching my session variables with Firebug . 最后,我在使用Firebug观察会话变量后找到了答案。 I noticed that on the pages that the variables were being lost, the session Parameter:secure was being set to true for some reason unknown to me. 我注意到在变量丢失的页面上,会话Parameter:secure由于某些我不知道的原因而被设置为true

The fix was to set the secure parameter to false before the session was created. 修复是在创建会话之前将secure参数设置为false

I accomplished this using session_set_cookie_params . 我使用session_set_cookie_params完成了这个。 Something like this: 像这样的东西:

session_set_cookie_params([lifetime], [path], [domain], false, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM