【发布时间】:2013-05-06 03:11:04
【问题描述】:
我在设置一个非常小的应用程序时遇到了一些麻烦。这将是一个小调查。
表格分为两页。提交第一个后,数据存储在$_SESSION数组中:
save_items(array('my_data' => $my_data_oject));
函数save_items() 如下所示:
function save_items(array $array) {
foreach ($array as $name => $item) {
$_SESSION[$name] = $item;
}
}
然后我unset($_POST) 并像这样重定向:
header('Location: index.php?action=survey_part2');
exit;
我的问题是:重定向后,之前存储的登录数据还在$_SESSION,但my_data_object不在。如果我避免重定向,我可以看到my_data_object 在重定向开始之前存储在$_SESSION 数组中。所以header() 和exit 的组合似乎部分地破坏了会话。有人知道这是怎么发生的吗?
最后,我的控制器的部分:
<?php
error_reporting(E_ALL);
session_start();
require_once 'models/functions.php';
require_once 'models/classes.php';
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
$view = $action;
$language = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : 'de';
switch ($action) {
case 'login' :
if ((!empty($_POST['email'])) && (!empty($_POST['password']))) {
$user = new User();
$login = $user->find_user($_POST);
if (!empty($login)) {
set_message('Welcome ' . $login['Firstname'] . ' ' . $login['Lastname'] . '!');
save_items(array('user_id' => $login['CID']));
unset($_POST);
redirect("index.php?action=survey&lang=de"); //<- works fine. Login is kept, Message is kept.
} else {
set_message('Please try again.');
unset($_POST);
}
} else {
unset($_POST);
set_message('Try again.');
}
break;
/* ... shortage */
case 'survey' :
check_login(); //<- doesn't matter
if (empty($_POST)) {
/* ... shortage */
} else {
/* ... creation of my_data_object + setting one more message */
save_items(array('my_data' => $my_data_object));
unset($_POST);
save_items(array('test' => 'you see me?')); //<- index.php?action=survey_2 won't get it
//var_dump($_SESSION);
header('Location: index.php?action=survey_2&lang=de'); //<- does not work. Login is kept in $_SESSION, but not my_data
exit;
}
break;
谢谢!
这个话题可能和that one here类似,但是我的$_SESSION在header()之后不是空的,而是被部分删除了。
【问题讨论】:
-
它的
header('Location: index.php?action=survey_part2');而不是unset($_POST); -
第一点是对的,我已经更正了。在我的代码中已经可以了。是否使用 unset($_POST) 没有任何影响。
-
您的页面
index.php?action=survey_part2上是否调用了session_start();?此外,在你的“控制器”末尾你有redirect('index.php?aktion=umfrage_2&lang=de');- 那不应该是header('Location: index.php?action=survey_part2'); -
1.: 是的。 index.php 是我的控制器,无论“action”具有哪个值,它都保持不变。 2.redirect()确实有歧义,是我自己的函数header('Location: index.php');出口;。我会编辑它。
-
可能是缓存相关的问题?
标签: php session redirect header