简体   繁体   中英

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>

<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.

PS: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM