使用会话在php页面之间传输数据

时间:2017-06-01 20:07:52

标签: php session

我正在尝试使用session在php页面之间传输数据。我的代码如下:

check_code.php:

<?php
session_start();
echo "<form action=\"check_code.php\" method=\"post\">";

echo "<h2>Your Name.  *</h2><input type='text' name='user_name'>";
echo "<br><br>";
echo "<h2>Your age.  *</h2><input type='text' name='age'>";
echo "<br><br>";
echo "<br><br><br>";
echo "<div><input type='submit' value='Review'></div>";
echo "</form>";
?>

<?php  
if((empty($_POST['user_name'])) || (empty($_POST['age'])) ) {
    echo "<h2>Please enter your user name and age</h2>";
} else {
    echo "<form action=\"page2.php\" method=\"post\">";
    $user_name = $_POST['user_name'];
    $age = $_POST['age'];
    echo "Below are the details entered:<br>";
    echo "Name: $user_name";
    echo "Age: $age";
    echo "Select any one: ";
    echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" 
      name="subject[]" value="Science">Science</td>';
    echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" 
      name="subject[]" value="Math">Math</td>';
    echo "<br>";

    $_SESSION['user'] = $_POST['user_name'];
    $_SESSION['age'] = $_POST['age'];
    $_SESSION['subject'] = $_POST['subject'];


    echo "<input type=\"submit\" value='Add to DB' >";
    echo "</form>";
}   

?>

使page2.php:

<?php
session_start();
$user_name =  $_SESSION['user'];
$age = $_SESSION['age'];
$subject = $_SESSION['subject'];
echo "<h2>Below are the details entered:</h2><br>";
echo "<h2>Name: </h2>$user_name";
echo "<h2>Age: </h2>$age";
echo "<h2>Subject selected: </h2>";
for ($i=0;$i<sizeof($subject);$i++) {
    echo "  $subject[$i]  ";
} 

?>

姓名和年龄显示在最后一页(page2.php)中。主题不会传递到下一页。我在这里犯了什么错误?

任何帮助将不胜感激!!!

1 个答案:

答案 0 :(得分:0)

您提供的代码存在一些问题,因此我重写了您的代码,您可以尝试下面的代码:

check_code.php 文件:

<?php session_start(); ?>
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <label>Your name</label>
    <input type="text" name="name" />
    <br>
    <label>Your age</label>
    <input type="number" name="age" />
    <hr>
    <button type="submit" name="review">Review</button>
    <?php if(isset($_SESSION['details'])) { ?>
    <button type="submit" name="unset">Unset</button>
    <?php } ?>
</form>
<?php
    if(isset($_SESSION['details'])) {
        if(isset($_POST['unset'])) { // If pressed "unset", remove the session and the values and restart
            unset($_SESSION);
            session_destroy();
        }
    }

    if(isset($_POST['review'])) {
        if(!empty($_POST['name']) && !empty($_POST['age'])) { // If fields are not empty
?>
<p>Your Details:</p>
<table>
    <tr>
        <td>Name<td>
        <td><?php echo $_POST['name']; ?></td>
    </tr>
    <tr>
        <td>Age<td>
        <td><?php echo $_POST['age']; ?></td>
    </tr>
</table>
<?php
            $_SESSION['details'] = array(
                'name' => $_POST['name'],
                'age'  => $_POST['age']
                // Storing them in array as $_SESSION['details'][name/age/whatever]
            );
        }
        else {
            echo 'Please fill in the fields.';
        }
    }

    if(isset($_SESSION['details'])) {
?>
<p><?php echo $_SESSION['details']['name']; /* Stored name in session */ ?>, Please Select Subject:</p>
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <label>Science</label>
    <input type="checkbox" name="subject[]" value="science" />
    <br>
    <label>Math</label>
    <input type="checkbox" name="subject[]" value="math" />
    <hr>
    <button type="submit" name="send">Remember My Choice</button>
</form>
<?php
        if(isset($_POST['send'])) { // If you send the second form, then...
            if(isset($_POST['subject'])) { // If selected subject
                $_SESSION['subject'] = array();
                for($i = 0; $i < count($_POST['subject']); $i++) {
                    $_SESSION['subject'][] = $_POST['subject'][$i]; // store all values of "subject" in the session
                }
            }

            header('location: page2.php');
        }
    }

<强>解释

您希望用户在提交表单后选择主题,并在用户无法检查主题行时定义它。当用户无法定义变量时,您可以继续 - 但是有错误 - 这就是我在尝试你的代码时所得到的。

所以我做的是以下步骤:

  1. 发送包含姓名和年龄的第一个表单
  2. 定义名为&#34的$_SESSION变量;详细信息&#34; (作为包含所需信息的数组)
  3. 如果此变量存在 - 则允许用户选择主题。
  4. 然后,当您选择一个(或多个)科目时,他们也会在会话中保存:

    page2.php 文件:

    <?php
        session_start();
        if(isset($_SESSION['details'])) {
    ?>
    <p>Your name is <?php echo $_SESSION['details']['name']; ?> and your age is <?php echo $_SESSION['details']['age']; ?></p>
    <?php if(isset($_SESSION['subject'])) { ?>
    <p>And your subject(s) are <?php echo implode(', ', $_SESSION['subject']); ?></p>
    <?php } else { ?>
    <p>You don't have any subject</p>
    <?php
            }
        }
        else {
            die('An Error Occurred');
        }
    

    page2.php我检查了是否设置了详细信息。如果是,那么我们可以继续检查是否也设置了主题。如果未设置详细信息,连接将死亡并打印错误消息。如果您没有设置主题,您也会收到有关该主题的消息。

    重要提示:

    • 你的代码,这个也很脆弱。除非您注意XSS保护,否则请勿在服务器中使用它们。您可以转义字符并使用正则表达式来消除&#34;消毒&#34;输入。

    您可以使用此代码替换当前代码。

    我希望它会有所帮助