2017-06-01 71 views
0

我想使用会话在php页面之间传输数据。我的代码如下:使用会话的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)。主题不会传递到下一页。我在这里犯了什么错误?

任何帮助将不胜感激!

+0

主题仅获取姓名和年龄都输入和相同的check_code后加入。PHP获取提交。 – mchapa

+0

您是否尝试过我的解决方案? – natanelg97

+1

嗨,是的,我已经尝试过,它的工作!谢谢!!! – mchapa

回答

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'); 
     } 
    } 

说明

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

所以我所做的是以下步骤:

  1. 发送第一种形式的姓名和年龄
  2. 定义$_SESSION变量命名为“细节”(如数组保持所需的信息)
  3. 如果此变量存在 - 则允许用户选择一个主题。

    使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保护,否则不要在服务器中使用它们。您可以转义字符并使用正则表达式来“消毒”输入。

你可以用这个替换你当前的代码。

我希望这将是有益的