I have an strange problem which I can’t find out what is causing this behaviour.
I have an page called dashboard.php where I have this code:
mail('[email protected]', 'Dashboard 1', 'isLoggedin = ' . $isLoggedIn);
if(!$isLoggedIn) {
header("Location: https://example.com/account/login.php");
}
mail('[email protected]', 'Dashboard 2', 'Dashboard 2');
If the user isn’t logged on the dasboard.php page, he must be redirected to the login.php page.
When I am not logged in and go to dashboard.php, the following happened:
- I got the first mail where isLoggedin is empty (which is correct ofcourse)
- I got redirected to the login.php page which is also correct
- I got the second ‘Dashboard 2’ mail which I don’t understand.
Why get I the second mail but I am redirected to the login.php so the script in dashboard.php shouldn’t get so far? Why is the script after the redirect still running?
2
Answers
Try to use exit(); or die(); to finish rendering page and go next to header. It might be solution.
Die in PHP
Exit in PHP
as shown in php.net docs, you have to
exit;
your script, otherwise it will be executed.