If no $_POST errors are found the variables are set to sessions and the page is redirected to itself to clear the post array. Once the page is reloaded from the redirect the sessions are set to standard variables and the sessions are unset and destroyed.
Unfortunately I'm back where I started. Everything works great in chrome, but browsers like firefox and safari are not storing the sessions. Once redirected the page in those browsers does not output any results from the form submission and has an empty form.
So just to be clear some browsers are having trouble handling the header redirect and storing the session data, but I do not understand why.
header("Location: /poop-calculator/",TRUE,303);
Everything works in all browsers as intended without the header redirect above. But I need to get this working to avoid duplicate submissions on page refreshes.
Here's what I'm currently working with:
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();
}
How about removing the Header and doing this is javascript?
replace header("Location: /poop-calculator/",TRUE,303);
with echo '<script>window.location="/poop-calculator";</script>'