我正在尝试为表单的每个字段创建一个会话,以便我可以在下一页的附加信息表单中重新调用它。
<?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 :(得分: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。
答案 1 :(得分: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();
}
?>
答案 2 :(得分: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];
}
}
?>