PHP header() gets sent after ouput
Until recently, my LAMP was working as I expected and using PHP's header() to redirect behaved according to documentation. Out of nowhere, this changed. The problem can be seen here:
<html>
<head>
<title> BLAH </title>
</head&开发者_运维知识库gt;
<body>
<p> TEXT </p>
<?php
echo "BLAH BLAH BLAH";
sleep(10);
header("Location: http://example.com");
?>
</body>
</html>
As far as I know, the redirect should not occur - but it does. I tried echoing, adding HTML, waiting to avoid any possible race conditions, all to no avail. When I access this page, no output is shown, and after 10 seconds I get redirected.
I wasn't very lucky searching the web as most people have exactly the opposite problem, nor did I find anything useful in the server logs. I haven't done any configuration changes or package updates. The people I asked were also puzzled.
I'm running PHP 5 and Apache 2 on a CentOS virtual machine.
P.S.: My first question here, but reading this site has helped me many times and I just love it :) .
Thanks Raoul, that was it.
I totally forgot I had been working on another Debian machine before the VM. And, of course, its default php.ini was different from that of the CentOS's.
Debian had output_buffering = Off
while CentOS was set to 4096.
Also, PHP errors were disabled on the VM.
Thanks again, guys, it's very reassuring to know it's not an obscure language quirk or an elusive design flaw. Cheers.
精彩评论