3

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?

7
  • How can I do it? Just simply click a feedback yes or no? Commented Aug 24, 2011 at 17:00
  • 2
    Turn on error_reporting, error_level, etc... in PHP. You'll most likely get an "Cannot send headers - output already started at line XXX" error. Commented Aug 24, 2011 at 17:04
  • 1
    @mario that is not what downvotes are meant for. since you like linking to the faqs, here's one for you too... Downvoting should be reserved for extreme cases. It's not meant as a substitute for communication and editing. Oh, and I will remove my answer, which actually helped him solve his question, but violates the standards. Commented Aug 24, 2011 at 17:23
  • @mario then make a comment for the poster to expand more... we're not here to make beginners feel uncomfortable posting questions. you think everyone knows innately about the "headers-already-sent" problem? if something isn't clear just ask for more details, get them involved, don't just start the downvote train. Commented Aug 24, 2011 at 17:36
  • @mario let us continue this discussion in chat Commented Aug 24, 2011 at 17:36

9 Answers 9

14

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Interestingly I had no die under header. When added die, redirect started to work hmm.
3

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

Comments

0

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.

1 Comment

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?
0

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.

Comments

0

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.

Comments

0

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.

Comments

0

Add

ob_start();

before

header("location: test.php");

Comments

0

In the original question the die() function is executed while there is a header() just before.
I had a similar problem so I created the code below to test.

header("Location:index.php");

$file = fopen("test.txt","a");
fwrite($file,date("Y-m-d H:i:s")."\n");
fclose($file);

And...? Well the code present after the header() function is executed and after only the header() returns to the index.php page.

All that to say that the header() function can be used at the end of a script to refer to another script.
But BE CAREFUL if this is positioned at the start of the script, it does not prevent the script from running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.