views:

80

answers:

4

So I just got a nasty surprise when I deployed some code I thought I'd tested. It would seem there must be some difference between my test machine and my server. The exact same code, featuring a header redirect, worked perfectly on my test machine and not at all on the server. The redirect on the server simply didn't happen, leaving a blank page as a result.

The header is called somewhere in the middle of the script - but nothing will have been output yet. It doesn't output anything until the very end of the script. Long after everything else is run. It buffers everything.

Both server and test machine are running the same PhP version, the same Apache version. Is there something in the configuration files that would allow the header to happen for one and not in the other? Is there something else going on here that would cause it to fail?

EDIT:

Here's the line that sets the header:

public function setRedirect($url) {
    header('Location: '.$url);
}

And here's the code that calls that:

$url = new URL('index');
$this->layout->setRedirect($url->toString());

Where URL::toString() always generates a fully qualified domain name, in this case: http://domain/index.php?action=index

I checked both Php and Apache error logs. Nada.

A: 

Use Fiddler or some other client-side tool to check your headers. Determine that the Location: header is actually being sent. Also, some browsers are picky in the order that headers need to be sent.

Brad
+3  A: 

Probably there was some whitespace or other form of output before the header call.

This is only work if you the ini setting output-buffering is on (or if you explicitly start output buffering, but in that case, the redirect should work in both computers).

You can confirm this by turning on error reporting.

Artefacto
If that was the case why would it work on the test machine and not the server? The output and scripts are identical. Also, error reporting showed nothing.
Daniel Bingham
@Daniel Because the server may have output buffering turned on and the test machine off.
Artefacto
@Artefacto Brilliant! Sorry, I misunderstood your answer the first time round, now I understand it. And it is indeed the case that the test server has output_buffering on and the server has it off. Lets see if this fixes it...
Daniel Bingham
@Artefacto Beautiful :) It worked like a charm. Wow, good call with that one, I would never have found that in a million years. Still sort of curious as to why errors never showed up, but as they say here in Thailand - mai bpen rai. Thanks!
Daniel Bingham
A: 

I think your server puts some script in your pages to track visitors and give you traffic stats or for a similar purpose. Ideally, you should get an error for this but may be your server has error reporting disabled which gives you a blank page.

I suggest you to run a script with a syntax error and check weather your server has error reporting disabled.

Shubham
A: 

I think the most likely explanation is that an error is causing the script to exit on your server, and you have display errors turned off (hence the blank screen). I would suggest checking the Apache error long on your server to see if PHP is putting something in there.

Otherwise you could use a browser extension like LiveHTTPHeaders (for Firefox) to see if the location header is being sent at all, or try debugging the script to see if it's even getting as far as that header call.

Tim Fountain