2015-04-17 153 views
0

我正在嘗試爲表單的每個字段創建一個會話,以便我可以在下一頁上的附加信息表單中調用它。會議不是由表單創建的?

<?php 
session_start(); // starting the session 

if (isset($_POST['Submit'])) { 

    $_SESSION['breakdowndate'] = $_POST['breakdowndate']; 
    $_SESSION['policyinception'] = $_POST['policyinception']; 
    $_SESSION['customername'] = $_GET['customername']; 
    $_SESSION['customersurname'] = $_GET['customersurname']; 
    $_SESSION['covertype'] = $_POST['covertype']; 
    $_SESSION['vehiclemake'] = $_POST['vehiclemake']; 
    $_SESSION['vehiclemodel'] = $_POST['vehiclemodel']; 
    $_SESSION['vehiclereg'] = $_POST['vehiclereg']; 
    $_SESSION['vehicleage'] = $_POST['vehicleage']; 
    $_SESSION['excess'] = $_POST['excess']; 
    $_SESSION['mileage'] = $_POST['mileage']; 
    $_SESSION['paid'] = $_POST['paid']; 
    $_SESSION['HSRS'] = $_POST['HSRS']; 
    $_SESSION['fault'] = $_POST['fault']; 
    $_SESSION['garage'] = $_POST['garage']; 
    $_SESSION['telephone'] = $_POST['telephone']; 

    } 

?> 

使用一些代碼和一些修改後,我有上述嘗試存儲已在形式提交的數據的代碼..

我怎樣才能改善這種代碼,使其工作?

+0

您是否將所有POST數據存儲到會話中? –

+0

表單完成後,數據存儲在數據庫中,但表單有兩個階段。初始階段捕獲您可以在上面的示例中看到的所有信息。然後用戶被帶到一個需要爲客戶添加額外信息的表單,我認爲這是做這件事的最好方法嗎?但我可能是錯的。如果有另一種方法可以將信息添加到剛剛創建的客戶端,那麼我接受建議。 – DGTLSS

+0

是提交此表單時轉到的頁面的下一頁?如果是這樣,如果這個表單的行爲是去那個頁面。我想你應該能夠訪問下一頁上的帖子值。你可以通過下面的例子來得到它:'」>'**請注意您的主機需要支持'php短標籤'**其他只是使用'<?php echo ...?>' – SuperDJ

回答

1

如果它不工作,請嘗試調試。

如果是保存POST,那麼剩下的事情就不多了。

session.cookie.secure設置爲true。您可能需要設置session.cookie_lifetime

就安全性而言,請考慮您是否有值得保護的東西。如果有人獲得訪問者的會話cookie,這有什麼關係嗎?如果沒有,忘記它。

session_set_cookie_params(0, '/', '', true, false); 
session_start(); 

error_reporting(E_ALL); // debug 

if (isset($_POST['Submit'])) { 
    $_SESSION['breakdowndate'] = $_POST['breakdowndate']; 
    $_SESSION['policyinception'] = $_POST['policyinception']; 
    $_SESSION['customername'] = $_GET['customername']; 
    $_SESSION['customersurname'] = $_GET['customersurname']; 
    $_SESSION['covertype'] = $_POST['covertype']; 
    $_SESSION['vehiclemake'] = $_POST['vehiclemake']; 
    $_SESSION['vehiclemodel'] = $_POST['vehiclemodel']; 
    $_SESSION['vehiclereg'] = $_POST['vehiclereg']; 
    $_SESSION['vehicleage'] = $_POST['vehicleage']; 
    $_SESSION['excess'] = $_POST['excess']; 
    $_SESSION['mileage'] = $_POST['mileage']; 
    $_SESSION['paid'] = $_POST['paid']; 
    $_SESSION['HSRS'] = $_POST['HSRS']; 
    $_SESSION['fault'] = $_POST['fault']; 
    $_SESSION['garage'] = $_POST['garage']; 
    $_SESSION['telephone'] = $_POST['telephone']; 
} else { 
    echo '<h3>Session Not Saved</h3>'; 
} 

echo htmlentities(var_export($_REQUEST, true)); // debug 
echo htmlentities(var_export($_SESSION, true)); // debug 

DEBUG

它應該工作 - 如果不是,測試和調試。

顯示所有警告。

設置後檢查$_SESSION

檢查請求。

$_REQUEST包括$_COOKIE它應該包含一個SESSION cookie。

0

我們需要重新生成id。嘗試這個。 (對不起,英語不好)

<?php 
session_start(); // starting the session 

if (isset($_POST['Submit'])) { 
    session_regenerate_id(); 
    $_SESSION['breakdowndate'] = $_POST['breakdowndate']; 
    $_SESSION['policyinception'] = $_POST['policyinception']; 
    $_SESSION['customername'] = $_GET['customername']; 
    $_SESSION['customersurname'] = $_GET['customersurname']; 
    $_SESSION['covertype'] = $_POST['covertype']; 
    $_SESSION['vehiclemake'] = $_POST['vehiclemake']; 
    $_SESSION['vehiclemodel'] = $_POST['vehiclemodel']; 
    $_SESSION['vehiclereg'] = $_POST['vehiclereg']; 
    $_SESSION['vehicleage'] = $_POST['vehicleage']; 
    $_SESSION['excess'] = $_POST['excess']; 
    $_SESSION['mileage'] = $_POST['mileage']; 
    $_SESSION['paid'] = $_POST['paid']; 
    $_SESSION['HSRS'] = $_POST['HSRS']; 
    $_SESSION['fault'] = $_POST['fault']; 
    $_SESSION['garage'] = $_POST['garage']; 
    $_SESSION['telephone'] = $_POST['telephone']; 
    session_write_close(); 
    } 

?> 
0

將你的變量存儲在數組中並循環,如下所示。您可以在您的下一頁上使用session_start();並訪問它們:

<?php 
     session_start(); // starting the session 
     $sessionItems = array('breakdowndate','policyinception','customername'...'telephone'); 

     if (isset($_POST['Submit'])) { 
      foreach($sessionItems as $item) { 
       $_SESSION[$item] = $_POST[$item]; 
      } 
      } 

     ?> 
+0

這似乎是(主觀)編寫相同代碼的更好方法。它是如何解決問題所問的問題? – Quentin