开发者

PHP Header("Location:...") works on one computer and not another

开发者 https://www.devze.com 2023-01-10 16:15 出处:网络
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

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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号