Search code examples
phphttp-redirectheaderlocation

PHP header() does not work


Does somebody know why my header() does not redirect?

The last part of my script is:

  header("location: test.php");
  die('died');

It writes:

died.

:(((

It should has to redirect before it dies, but it does not.

Do you have you any idea?


Solution

  • It's probably that you're calling header() after you are outputting some text/HTML to the browser, which is a no-no. Your header() call will be ignored if even so much as a single space of output has been sent before the call.

    In other words, your header() code needs to be at the start of your script, before you display anything to the user. If that still isn't working, make sure you don't have any spaces or other whitespace by mistake outside of your php tags.