0

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)
  2. Page1.php creates a session variable and if the session variable is set it then sends the browser to Page2.php
  3. On page2.php the browser is supposed to display the value of the session variable set in Page1. php
  4. My problem is that page2.php views the session variable as not set.
  5. I have tried all the solutions posted by other users on stack overflow as you can see from my code below:

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!";
?>
4

2 回答 2

2

session_id() 需要在 session_start() 之前调用

如果指定了id,它将替换当前会话 id。为此,需要在 session_start() 之前调用 session_id()。根据会话处理程序,会话 ID 中不允许所有字符。例如,文件会话处理程序仅允许 az AZ 0-9 、(逗号)和 -(减号)范围内的字符!

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

session_id()-手动

您还可以检查是否设置了基于 cookie 的身份验证。

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

于 2012-07-09T14:48:55.973 回答
1

在 上page2.php,交换前 2 行。改变

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

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

请参阅此处的Parameters部分..http://php.net/manual/en/function.session-id.php

于 2012-07-09T14:52:28.650 回答