简体   繁体   中英

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?

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.

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');

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.

I was stuck on this error, too, and tried to show errors with error_reporting(E_ALL), but this didn't worked for me. Info: I was using a hosting service on Strato.

The solution to all this was answered by Floem, thanks for this great setup.

If you ever don't get errors in case you put error_reporting(E_ALL) once in, -> You must additionally add ini_set('display_errors', 'On') , to force PHP to show all your Errors. This solved my Error issue on

Then it's possible to read out, why PHP wouldn't make your redirect. One tip on my side: I would recommend you to build a little redirect Class, as shown in this Video: Redirect Class for easy use , if you're going to use this redirect more than usual.

Just resolve it by removing ALL things before the php tag.

Remove the SPACE before the php tag

Select all before the tag and then push the erase button.

If the file containing the 'header()' instruction is required directly/indirectly in a HTML file, then the instruction {include 'someFile'} should be the first line of that HTML file. Place this instruction on the top of your HTML file, as shown in this image.

Add

ob_start();

before

header("location: test.php");

location: should be Location: .

Moreover...

  • Scroll to the top of this page.
  • Click on your account name.
  • Click on a random question
  • Check mark a random answer
  • Come back to find more serieus answers

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM