2017-06-01 75 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保護,否則不要在服務器中使用它們。您可以轉義字符並使用正則表達式來「消毒」輸入。

你可以用這個替換你當前的代碼。

我希望這將是有益的