PHP header() does not work

17,485

Solution 1

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.

Solution 2

Maybe there is some invisible output before header, which is preventing setting header, and informative warnings are suppressed.

Additionally Location-Headers should contain an absolute URL:

// Show Errors
error_reporting(E_ALL);
ini_set('display_errors','On');
// Redirect
header('Location: http://example.com/path/test.php');
die('redirect');

Solution 3

You should use a fully-qualified address for your location header, and not output any content:

header('Location: http://example.com/test.php');
die();

Additionally make sure that no other content was sent before setting the header, otherwise it wont be sent as the headers will have already been sent to the browser.

Share:
17,485
szatti1489
Author by

szatti1489

Updated on June 04, 2022

Comments

  • szatti1489
    szatti1489 almost 2 years

    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?

  • szatti1489
    szatti1489 over 12 years
    Dear MattK, When you coding for a long-long hours and a long-long code created, and your code before a presentation and off course the error reports are turned off, and the f.. header doesnt redirects though a f... echoed empty string... What do you do in this case?