会话变量从一个页面变为另一个页面

时间:2014-01-28 03:04:33

标签: php

我在一个页面上有以下代码

<?php
// Account for the possibility of time out
session_start();
$_SESSION['LoggedIn']=$_GET['LoggedIn'];
$_SESSION['SetName']=$_GET['SetName'];
    $setName=$_SESSION['SetName'];
$_SESSION['UserName']=$_GET['UserName'];
if (!isset($_SESSION['LoggedIn']) || !$_SESSION['LoggedIn']) header("Location: Home.php");
    php var_dump($_SESSION); 
    header("Location: uploadFiles.php"); // DEBUG
?>

如果标题(“Location:uploadFiles.php”);被注释掉了,这给出了

array(3) { ["LoggedIn"]=> string(4) "TRUE" ["SetName"]=> string(4) "test" ["UserName"]=> string(5) "OtagoHarbour" } 

uploadFiles.php具有以下代码

<?php
// Account for the possibility of time out
session_start();
if (!isset($_SESSION['LoggedIn']) || !$_SESSION['LoggedIn']) {
    php var_dump($_SESSION);
    ?>
    <script type="text/javascript">
    alert("Not logged in.  Session log in=<?php echo $_SESSION['LoggedIn'] ?>");
    document.location.href="Home.php";
    </script>
    <?php
}
?>

我收到警报

Array(0) {}
Not logged in.  Session log in=

2 个答案:

答案 0 :(得分:2)

您不会在uploadFiles.php中看到会话数据,因为您实际上并没有将它们打印出来。在调试echo时,您需要使用var_dump甚至更好,因为这会突出显示空变量。你的JS语法也是不正确的 - PHP的输出需要在里面引号,否则会导致语法错误。请记住,如果会话var包含语音标记,则需要转义:

<?php
// Account for the possibility of time out
session_start();
if (!isset($_SESSION['LoggedIn']) || !$_SESSION['LoggedIn']) {
    ?>
    <script type="text/javascript">
    alert("Not logged in.  Session log in=<?php var_dump( $_SESSION['LoggedIn'] ); ?>");
    document.location.href="Home.php";
    </script>
    <?php
}
?>

答案 1 :(得分:0)

我没有提到我正在使用LAMP。我也没有检查

/var/log/apache2/error.log

它有消息

PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0, referer: http://whatever.com/uploadFiles.php

以下解决了问题

sudo chown www-data /var/lib/php5