Nate
Nate

Reputation: 28384

Header redirect being VERY funky.. What's going on?

This is really weird.. I have the following redirect in a file:

header('Location: http://google.com');

It doesn't work. The page loads and the user is not redirected. However, if I add a die() statement below that line, like this:

header('Location: http://google.com');
die('what the heck is going on here?');

Then the redirect magically starts working!

I'm at a total loss as to why this is happening, and I have no idea how to debug it.

Can anyone give me any advice on this?

Upvotes: 1

Views: 51

Answers (2)

arijeet
arijeet

Reputation: 1874

Probably your script isn't ending after the header call.. Make sure you end the script.

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318498

You are supposed to exit after sending a location header. Sending any header does not affect the flow of the script so any code after it still executes. If anything causes a different response status code to be set the redirect will not happen at all.

A Location header by itself does nothing - only together with the proper 30x response code it will cause a redirect. PHP sets this response code manually when sending a Location header but your code might modify it.

Upvotes: 7

Related Questions