PHP头重定向在Firefox和Safari中清除会话数据?

时间:2015-06-16 18:57:35

标签: php forms redirect post get

如果未找到$ _POST错误,则将变量设置为会话,并将页面重定向到自身以清除post数组。从重定向重新加载页面后,会话将设置为标准变量,并且会话将被取消设置并销毁。

不幸的是,我回到了我开始的地方。一切都在chrome中很好用,但像firefox和safari这样的浏览器并没有存储会话。重定向后,这些浏览器中的页面不会从表单提交中输出任何结果,并且表单为空。

所以只是要清楚一些浏览器在处理标题重定向和存储会话数据时遇到问题,但我不明白为什么。

header("Location: /poop-calculator/",TRUE,303);

一切都适用于所有浏览器,没有上面的标题重定向。但我需要这样做以避免页面刷新时重复提交。

以下是我目前正在使用的内容:

session_start();error_reporting(E_ALL); ini_set('display_errors',1);


if(count($_POST) > 0) {


//Error Checking


$EC = 0;


if (isset($_POST['submit'])&&empty($_POST['dob-month'])){
$EC++;
$errors[$EC] = "Select a month"; 
}
if (isset($_POST['submit'])&&empty($_POST['dob-day'])|| $_POST['dob-day'] < 1 || $_POST['dob-day'] > 31){
$EC++;
$errors[$EC] = "Enter a day between 1 and 31";
}
if (isset($_POST['submit'])&&empty($_POST['dob-year'])|| $_POST['dob-year'] < 1900 || $_POST['dob-year'] > date('Y')){
$EC++;
$errors[$EC] = "Enter a year between 1900 and ".date('Y');
}


if(!empty($errors)){
echo '<div class="woocommerce-error">
<div class="errorSpacer"></div>';


foreach($errors as $error){
echo '<div class="error-wrapper">
         <div class="erroWrapperLeft">
         <p class="errorIcon"></p>
            </div>
            <div class="erroWrapperRight">
             <p class="errorText">'.$error.'</p>
            </div>
       </div>';
  }
echo "</div>";
} 


//set sessions


if(empty($errors)&& isset($_POST['submit'])){
(isset($_POST['dob-month'])? $_SESSION['dob-month'] = $_POST['dob-month']:$_SESSION['dob-month'] = null);
(isset($_POST['dob-day'])? $_SESSION['dob-day'] = ltrim(sanitizeNumInput($_POST['dob-day']),'0'):$_SESSION['dob-day'] =  null);
(isset($_POST['dob-year'])?$_SESSION['dob-year'] = sanitizeNumInput($_POST['dob-year']):$_SESSION['dob-year'] =null);
(isset($_POST['submit'])?$_SESSION['submit'] = 1:$_SESSION['submit'] = null);


header("Location: /calculator/",TRUE,303);
}


//set variables and clear sessions


    }
if (isset($_SESSION['dob-month'])||isset($_SESSION['dob-day'])||isset($_SESSION['dob-year'])||isset($_SESSION['submit'])){
        (isset($_SESSION['dob-month'])? $month = $_SESSION['dob-month']:$month = null);
(isset($_SESSION['dob-day'])? $day = ltrim(sanitizeNumInput($_SESSION['dob-day']),'0'): $day = null);
(isset($_SESSION['dob-year'])?$year = sanitizeNumInput($_SESSION['dob-year']):$year=null);
(isset($_SESSION['submit'])?$submit = $_SESSION['submit']: $submit = null );




        session_unset();
        session_destroy();


    }

1 个答案:

答案 0 :(得分:1)

如何删除标题并执行此操作是javascript?

替换header("Location: /poop-calculator/",TRUE,303);
echo '<script>window.location="/poop-calculator";</script>'