skip to Main Content

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:

  1. I got the first mail where isLoggedin is empty (which is correct ofcourse)
  2. I got redirected to the login.php page which is also correct
  3. 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


  1. 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

    Login or Signup to reply.
  2. as shown in php.net docs, you have to exit; your script, otherwise it will be executed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search