简体   繁体   English

标头重定向后会话变量丢失

[英]Session variables lost after header redirect

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. I have two pages (page1.php, page2.php) 我有两个页面(page1.php,page2.php)
  2. Page1.php creates a session variable and if the session variable is set it then sends the browser to Page2.php Page1.php创建一个会话变量,如果设置了会话变量,则将浏览器发送到Page2.php
  3. On page2.php the browser is supposed to display the value of the session variable set in Page1. 在page2.php上,浏览器应该显示在Page1中设置的会话变量的值。 php 的PHP
  4. My problem is that page2.php views the session variable as not set. 我的问题是page2.php将会话变量设置为未设置。
  5. I have tried all the solutions posted by other users on stack overflow as you can see from my code below: 我已经尝试了其他用户在堆栈溢出时发布的所有解决方案,如下面的代码所示:

Page1.php 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 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() needs to be called before session_start() 需要在session_start()之前调用session_id()

If id is specified, it will replace the current session id. 如果指定了id ,它将替换当前的会话ID。 session_id() needs to be called before session_start() for that purpose. 为此,需要在session_start()之前调用session_id()。 Depending on the session handler, not all characters are allowed within the session id. 根据会话处理程序的不同,会话ID内不允许所有字符。 For example, the file session handler only allows characters in the range az AZ 0-9 , (comma) and - (minus)! 例如,文件会话处理程序仅允许az AZ 0-9范围内的字符,(逗号)和-(减号)!

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. 注意 :使用会话cookie时,为session_id()指定一个ID总是在调用session_start()时发送一个新的cookie,无论当前会话ID是否与所设置的ID相同。

session_id()-Manual session_id()-手册

You've also might check whether you'll have cookie based authentication set. 您可能还会检查是否要设置基于Cookie的身份验证。

Be aware that if users post the url, they might carry the session to another client. 请注意,如果用户发布该URL,则他们可能会将会话带到另一个客户端。

On page2.php , interchange the first 2 lines. page2.php ,交换前两行。 Change 更改

session_start();
session_id($_GET['PHPSESSID']);

to

session_id($_GET['PHPSESSID']);
session_start();

See the Parameters section here..http://php.net/manual/en/function.session-id.php 请参阅此处的“ Parameters部分。.http://php.net/manual/zh/function.session-id.php

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

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