I suspected this wouldn’t work, and it didn’t – but I can’t seem to find a similar question anywhere so here we go. I had a if else statement that checked to see if they couldn’t log in it would show why (errors), and if there were, they would be echoed out, and otherwise, $_SESSION would be assigned a user id (having already been started beforehand) that was retrieved from a variable that equates to a function which queries the mySQL db. and the page would redirect to an account page, as so:
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error . '<br />'; .
}
} else {
$_SESSION['user_id'] = $login;
header('Location: you/default.php');
exit();
}
The problem is this login form is in a dropdown menu on the homepage (rather than being a page in it’s own right), and if they can’t log in (incorrect password, an unfilled field, etc), I want to redirect to the login.php page and then display the errors, so I tried adding header('Location: login.php'); before the foreach statement, but it just redirected to login.php, like I expected, and didn’t bring the errors along with it.
I suspect this may be a job for AJAX or something other than PHP… ideas?
I actually figured out a way to do this myself. In my case if there were errors (
if (!empty($errors))), I applied them to the$_SESSIONsuperglobal as$_SESSION['errors'].It then redirects the page to
login.phpwhere a simple if statement echos them out there:Easy!