为什么我在 PHP 中的第一个会话没有向第二个会话发送数据?

Why is my first session in PHP not sending data to my second?

我正在尝试让一个会话将页面中的数据输入到 post 数组,然后将其存储到会话数组,然后将其发送到会话 2,我希望将信息发送到会话 2被显示。我的第一个会话将我发送到我的会话 2 页面,但没有显示信息。谁能告诉我我的代码是否有问题?谢谢

第 1 节

<?php
session_start();

if(isset($_POST["employeeNameBox"]))
{
    $_SESSION["employeeName"] = $_POST["employeeNameBox"];
    header("Location: Session2.php");
}

if(isset($_POST["employeeidBox"]))
{
    $_SESSION["employeeid"] = $_POST["employeeidBox"];
    header("Location: Session2.php");
}

if(isset($_POST["phoneNumberBox"]))
{
    $_SESSION["phoneNumber"] = $_POST["phoneNumberBox"];
    header("Location: Session2.php");
}

if(isset($_POST["emailBox"]))
{
    $_SESSION["email"] = $_POST["emailBox"];
    header("Location: Session2.php");
}

if(isset($_POST["positionBox"]))
{
    $_SESSION["position"] = $_POST["positionBox"];
    header("Location: Session2.php");
}

if(isset($_POST["ITProjectBox"]))
{
    $_SESSION["ITProject"] = $_POST["ITProjectBox"];
    header("Location: Session2.php");
}

?>

<html>

<body>

<?php include('Header.php'); ?>
<?php include('Menu.php'); ?>

<div class="content">

            <form method="POST">
        
            <input type="text" name="employeeNameBox" value="Employee name"> <br> 
            <input type="text" name="employeeidBox" value="Employee id"> <br>
            <input type="text" name="phoneNumberBox" value="Phone number"> <br> 
            <input type="text" name="emailBox" value="Email"> <br> 
            
            <input type="radio" id="Manager" name="positionBox" value="Manager"> 
            <label for="Manager">Manager</label><br>
            <input type="radio" id="Team Lead" name="positionBox" value="Team Lead"> 
            <label for="Team Lead">Team Lead</label><br> 
            <input type="radio" id="IT Developer" name="positionBox" value="IT Developer"> 
            <label for="IT Developer">IT Developer</label><br>
            <input type="radio" id="IT Analyst" name="positionBox" value="IT Analyst"> 
            <label for="IT Analyst">IT Analyst</label><br><br>


            <select name="ITProjectBox">
                <option value="Project A">Project A</option>
                <option value="Project B">Project B</option>
                <option value="Project C">Project C</option>
                <option value="Project D">Project D</option>
            </select> <br><br>

            <button type="submit">Submit Information</button>
            
            </form>

</div>


</div>

<?php include('Footer.php'); ?>


</body>
</html>

第 2 节

<?php
session_start();

if(isset($_SESSION["employeeName"]))
{
    echo "<b>Employee name: </b>" .$_SESSION["employeeName"];
    echo("<br><br>");
}

if(isset($_SESSION["employeeid"]))
{
    echo("<b>Employee id: </b>" .$_SESSION["employeeid"]);
    echo("<br><br>");
}

if(isset($_SESSION["phoneNumber"]))
{
    echo("<b>Phone number: </b>" .$_SESSION["phoneNumber"]);
    echo("<br><br>");
}

if(isset($_SESSION["email"]))
{
    echo("<b>Email: </b>" . $_SESSION["email"]);
    echo("<br><br>");
}

if(isset($_SESSION["position"]))
{
    echo("<b>Position: </b>" . $_SESSION["position"]);
    echo("<br><br>");
}

if(isset($_SESSION["ITProject"]))
{
    echo("<b>IT Project: </b>" . $_SESSION["ITProject"]);
    echo("<br><br>");
}

?>

<html>

<body>

<?php include('Header.php'); ?>
<?php include('Menu.php'); ?>

<div class="content">

</div>

<?php include('Footer.php'); ?>


</body>
</html>

首先,确保您的会话在重定向之前已保存,您可以使用 session_write_close()。

其次,确保您的脚本在重定向后立即完成,使用退出。

您可以使用一个同时执行这两个操作的函数来节省代码:

session_start();

if(isset($_POST["employeeNameBox"]))
{
    $_SESSION["employeeName"] = $_POST["employeeNameBox"];
    my_redirect('Session2.php');
}

//Here rest of vars

function my_redirect($to)
{
    session_write_close();
    header('Location: ' . $to);
    exit;
}