标头重定向后会话变量丢失 [英] Session variables lost after header redirect

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

问题描述

如果这看起来与此处发布的其他问题相似,请忍受我,我已经完成了所有提供的答案,但尚未解决我的问题.我已将问题减少到最低限度.

Bear with me if this looks similar to other questions posted here, I have already gone through all answers provided but not has solved my problem. I've reduced my problem to the bare minimum.

  1. 我有两个页面(page1.php,page2.php)
  2. Page1.php创建一个会话变量,如果设置了会话变量,则将浏览器发送到Page2.php
  3. 在page2.php上,浏览器应该显示在Page1中设置的会话变量的值. php
  4. 我的问题是page2.php认为会话变量未设置.
  5. 我已经尝试了其他用户在堆栈溢出时发布的所有解决方案,如下面的代码所示:


Page1.php

<?php
//start the session
session_start();

//set the session
$_SESSION['mysession'] = "Hello";


if(isset($_SESSION['mysession'])){
    //redirect the person to page 2
    session_write_close();
    header("Location: page2.php?PHPSESSID=".session_id());
    exit();
} else {
 echo "Session Not Set";
}
?>

Page2.php

<?php
//start the session
session_start();
session_id($_GET['PHPSESSID']);

if ( isset ($_SESSION['mysession']) )
   echo $_SESSION['mysession'];
else
   echo "Session not set!";
?>

推荐答案

session_id()必须在session_start()之前调用

session_id() needs to be called before session_start()

如果指定了 id ,它将替换当前的会话ID. 为此,需要在session_start()之前调用session_id() 目的.取决于会话处理程序,并非所有字符都是 会话ID内允许.例如,文件会话处理程序 仅允许a-z A-Z 0-9范围内的字符,(逗号)和- (减号)!

If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose. Depending on the session handler, not all characters are allowed within the session id. For example, the file session handler only allows characters in the range a-z A-Z 0-9 , (comma) and - (minus)!

注意:使用会话Cookie时,请为session_id()指定一个ID 会在调用session_start()时始终发送新的cookie, 无论当前会话ID是否与当前会话ID相同 设置.

Note: When using session cookies, specifying an id for session_id() will always send a new cookie when session_start() is called, regardless if the current session id is identical to the one being set.

session_id()-手册

您还可能会检查是否要设置基于cookie的身份验证.

You've also might check whether you'll have cookie based authentication set.

请注意,如果用户发布url,他们可能会将会话带到另一个客户端.

Be aware that if users post the url, they might carry the session to another client.

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

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