PHP Header location not working in Internet Explorer IE6, IE7, IE8 [SOLUTION]

When I uploaded a custom forum I wrote to our production server, the header("Location..") call would not work in Internet Explorer (IE) browsers (IE6,IE7,IE8). It worked in some cases, but never in cases when they were POST processing pages.

The issue was with header("Location: ...");

We are running an IIS6, PHP 5.2.9 server and for some reason when I tried using header("Location ..") on pages that processed a form (via POST), the header("Location ...") call would stop page execution and return a "Cannot display page error", it wasn't a 404 either.

After spending a good amount of time figuring out if it was just an error due a configuration issue on the production server, it turned out that all I had to do was add "die();" after the header("Location..."); call. For some reason this worked for all the IE browsers I tested. It also continued to work in Firefox. So in the code:

header("Location: http://www.google.com"); //you do not have to use an absolute path, this is just an example
die();

I don't know why that made it work, but it did, I hope that helps you. Also if you are totally stuck, another solution is to use location.href via javascript. This is a sloppier approach but it works.



Note:
It goes without saying that when you develop on a local testing server, the remote testing server could have a significantly different configuration, so this issue may also come up if your code errors out on the production server and you simply cannot see the error. A symptom of this will result in a blank page, not a "Cannot display page error". So make sure you consider production side code errors when troubleshooting. The reason why you may not see the error and just a blank page is because it is good practice to NOT show errors and warnings on a production server. So a blank page is a good indicator that there is an error in your code.

Keywords: header location php, ie not working


Submit a Comment






Comments

Venu V

Posted on: Dec 21, 2012 10:17 pm

You saved lot of time. Great article. Keep on posting..

PerB

Posted on: Feb 17, 2011 5:24 pm

David's ("Location:" to "Location: ") tip saved me. Already had die() in place. On IIS you might also observe double-loading of pages when posting to yourself (serverside and clientside embedded within same file)

Bill

Posted on: Oct 13, 2010 9:02 pm

Awesome, you just saved me a ton of headaches. Thank You!

Al

Posted on: May 26, 2010 6:55 am

Thank you thank you thank you!

Nathan

Posted on: Mar 25, 2010 2:38 am

Wow, I`m loving this.

I had the exact same issue with some PHP location redirects. Where it was working perfectly in all browsers (that I tested) except for IE 6

Phil

Posted on: Jul 29, 2009 9:04 am

I wish I knew, IE always seems to be the black sheep when it comes to conformity testing and I was not surprised when it handled PHP Headers incorrectly.

David

Posted on: Jul 29, 2009 8:26 am

Cool just what I was looking for, also note/make sure there is a physical space after ("Location:" to "Location: ") as that caught me out on IE 7. Question is why is IE so pants, FF runs fine.