我$_SESSION
用来存储来自多页表单的数据。一切都很顺利,直到我完成提交并结束会话以防止在该人尝试重新输入表单时复制相同的表单数据。表单完成并提交后,终止会话并擦除会话数据:
$_SESSION = array();
setcookie(session_name(), '', time() - 42000);
session_destroy();
接下来是这样的:
<?php
session_start();
include "connect-moo.php";
$learning = true;
//get any post variables and save them to the session
foreach ($_POST as $key => $val) {
$_SESSION[$key] = $val;
}
sessionDump(); //dumps session to db
//check to see if step is set (in the post eg ?step=getaquote). If not, set it to current session step
if(!isset($_POST["step"])) {
if(isset($_SESSION['step'])) {
$_POST['step'] = $_SESSION['step'];
}
}
//show the page appropriate to the current step
if ($_SESSION['mode'] == 'edit' && $_SESSION['lastpage'] != "review" && $_SESSION['step'] != "session") {
$_SESSION['lastpage'] = 'review';
getReview();
}
else {
if($_SESSION['step'] != 'session') $_SESSION['lastpage'] = $_SESSION['step'];
switch ($_POST["step"])
{
case "session":
foreach($_SESSION as $key => $val){
echo $key.": ".$val."<br>";
//phpinfo();
}
echo "Post Data<br>";
foreach($_POST as $key => $val) {
$sval = mysql_real_escape_string($val);
$skey = mysql_real_escape_string($key);
echo $skey.": ".$sval."<br>";
}
break;
case "getaquote":
getAQuote();
break;
// and so on...
default:
start();
}
}
... (all of the abovementioned functions)
?>
我已经确认正在建立一个新的会话 cookie,但在重新启动会话$_POST
后$_SESSION
它是空的。通过最近的修复(谢谢!)我可以以编程方式将数据推送到 $_POST 和 $_SESION 数组中(例如 $_POST['foo'] = 'bar';),但通过 URI 传递的变量会被忽略。
解决方案:
重新 session_destroy,请参阅 Alex B 的答案。
回覆。$_POST 问题 - 事实证明,当直接通过 URL 输入信息时,服务器将其(正确)解释为 GET 而不是 POST。我修改了我的代码以获取所有 GET 和 POST 数据,但在生产中只需要 POST。这是新代码:
//get any submitted variables and save them to the session
foreach ($_GET as $key => $val) {
$_SESSION[$key] = $val;
}
foreach ($_POST as $key => $val) {
$_SESSION[$key] = $val;
}