【问题标题】:PHP Session variables are lost after moving to a pagePHP 会话变量在移动到页面后丢失
【发布时间】:2014-04-03 01:49:50
【问题描述】:

我在 PHP 中丢失会话变量时遇到了很多麻烦。我找到了很多很好的信息,但在这种情况下没有任何帮助。这是我的 PHP 文件:

首先,util.php。它包含在每个文件的顶部。它有 session_start() 命令和 MySQL 连接字符串:

<?php
    session_start();
    $connect=mysqli_connect("localhost","mysql","mypassword","mydb");
    if(mysqli_connect_errno()){
        echo 'Failed to connect to database ' . mysqli_connect_error();
    }
?>

接下来,show_variables.php。它会在屏幕上转储所有会话变量:

<?php
echo date('H:i:s');
echo "<br/><p>Session Variables:</p>";
foreach ($_SESSION as $key=>$val)
    echo $key." ".$val."<br/>";
echo session_name();
?>

现在是第一页,index.php。页面顶部从数据库表中提取员工姓名。正文呈现一个包含员工姓名和两个角色的表格。页面根据选择的角色重定向。

<?php
    include "util.php";
    $query=mysqli_query($connect,"select * from staff");
    while ($row = mysqli_fetch_array($query)){
        $staff_select .= '<option value="'.$row[id].'">'.$row[name_first].' '.$row[name_last].'</option>';
    }
?>
<html>
<head>
    <title>Restaurant Order Tracking</title>
    <link href="./css/style.css" rel="stylesheet" type="text/css">
    <script language="javascript" type="text/javascript">
        function start_shift(){
            var e_select = document.selector.employee_select;
            var name = e_select.options[e_select.selectedIndex].value;
            var r_select = document.selector.role_select;
            var role = r_select.options[r_select.selectedIndex].value;
            alert("Ok, " + name + ". You're a " + role + " today. Let's get to work.");
            if(role == "server"){location = "./server/server.php?id=" + name};
            if(role == "foodprep"){location = "foodprep.php?id=" + name};
        }
    </script>
</head>
<body>
    <div id="header">
        <h1>Welcome To The Restaurant Order Tracker</h1>
    </div>
    <div id="middle">
        <p>Now get to work !</p>
        <form name="selector">
            <p>Select your employee:</p>
            <select name="employee_select">
                <?php
                  echo $staff_select;
                ?>
            </select>
            <p>Select your role for today</p>
            <select name="role_select">
                <option value="server">Server</option>
                <option value="foodprep">Food Prep</option>
            </select>
            <br/>
            <input type="button" value="Start Shift" onclick="start_shift()"/>
        </form>
    <?php include "show_variables.php";?>
    </div>
</body>
</html>

此页面运行良好。在此处选择服务器会导致“server.php”。这是那个文件:

<?php
    include "../util.php";
    $_SESSION['id']=$id=$_GET['id'];
    $_SESSION['foo']="fubar";
    $query = mysqli_query($connect,"select name_first, name_last from staff where id = $id");
    while($row = mysqli_fetch_array($query)){
        $_SESSION['name'] = $row['name_first'].' '.$row['name_last'];
        }
?>
<html>
<head>
    <Title>Server Interface</Title>
    <link href="../css/style.css" rel="stylesheet" type="text/css">
    <script language="JavaScript" type="text/javascript">
        function update(target){
            var ajaxRequest;
            ajaxRequest = new XMLHttpRequest();
            ajaxRequest.onreadystatechange = function(){
                if(ajaxRequest.readyState == 4){
                    var middleDisplay = document.getElementById('middle');
                    middleDisplay.innerHTML = ajaxRequest.responseText;
                    }
                }
            ajaxRequest.open("GET", target, true);
            ajaxRequest.send(null);
        }
    </script>
</head>
<body>
    <div id="header">
        <p>Server Interface</p>
        <p>Server Name: <?php echo $_SESSION['name'] ?>  </p>
        <form action="../logout.php" method="post">
        <input type="submit" value="Logout">
        </form>
    </div>
    <div id="middle">
        <?php include "stage1.php" ?>
    </div>
</body>
</html>

此页面设置了三个变量:id、name 和 foo。正如预期的那样,show_variable.php 具有以下输出:

19:54:34
Session Variables:
id 2
foo fubar
name Dave Smith
PHPSESSID

在中间的 div 中还有一个包含 stage1.php。这是那个文件:

<br/>
<form name="selector">
    <input type="button" value="Start a new order" name="new_order" onclick = "update('new_order.php')" />
    <input type="button" value="View your open orders" name="view_order" onclick="update('view_order.php')" />
</form>
<?php include "../show_variables.php"; ?>

到目前为止,一切都很好。有一些 AJAX 可以更改中间 div,效果很好。如果用户选择“开始新订单”,AJAX 会将 new_order.php 放在中间 div 中。这是那个文件:

<?php
    include "../util.php";
    $query=mysqli_query($connect,"select * from loc");
    while ($row = mysqli_fetch_array($query)){
        $table_select .= '<option value="'.$row[id].'">'.$row[description].'</option>';
    }
    echo "<br/>
    <form name = 'new_order'>
    <label>Enter the table number for this order</label>
        <select name = 'table_select'>
            $table_select
        </select>
        <br/>
        <input type='button' value='Start New Order' onclick=\"update('enter_order.php?table=' + document.new_order.table_select[document.new_order.table_select.selectedIndex].value)\" />
        <br/>
        <input type='button' value='Cancel New Order' onclick=\"update('stage1.php')\" />
    </form>
<a href= "stage1.php">Go back to stage 1 via link</a>
"
?>
<?php include "../show_variables.php"; ?>

此页面还显示了正确的会话变量:

19:55:27
Session Variables:
id 2
foo fubar
name Dave Smith
PHPSESSID

但在那之后,所有的变量都消失了。如果用户单击“取消新订单”,AJAX 脚本会再次使用 stage1.php 填充中间 div。并且会话变量不再存在:

19:59:12
Session Variables:
PHPSESSID

这不能是 stage1.php 文件中的拼写错误。完全相同的代码在一个视图中具有会话变量,但在另一个视图中丢失了它们。 我想也许 AJAX 把我搞砸了,所以我将 href 添加到 new_order.php。没有任何 AJAX/JavaScript 直接进入 stage1.php。但是变量再次丢失了。

我尝试手动输入网址。 我浏览到http://www.mysite.com/server/server.php?id=1。会话变量正确显示。 我打开一个新标签并浏览到http://www.mysite.com/server/server.php(没有获取)。会话变量存在并且设置相同。 我打开另一个选项卡并浏览到http://www.mysite.com/server/stage1.php。会话变量不存在。

请注意,每个文件都有通过 util.php 文件的 session_start() 命令。另请注意,这是一个开发服务器。我的是网站上唯一的会话。有什么想法吗?

【问题讨论】:

标签: php ajax session


【解决方案1】:

从您发布的代码中不清楚,但在我看来,stage1.php 没有调用调用 session_start 的 util.php。

所以 stage1.php 似乎没有 session_start

为避免双重包含,您可以使用 include_once

【讨论】:

  • 啊,就是这样。 stage1.php 第一次只是包含(粘贴)在 server.php 中,因此它只是 server.php 的一部分。但是当 AJAX 脚本显式调用 stage1.php 时,它的行为就好像它是一个独立的文件。因此,server.php 中的“include util.php”命令不会被执行。谢谢。您不仅找到了一个错字,还帮助我更好地理解了 PHP 的逻辑。
猜你喜欢
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 2018-11-30
  • 2012-06-15
  • 2014-08-14
相关资源
最近更新 更多