PHP头重定向工作,尽管以前的HTML和echo输出


PHP Header Redirect Working Despite Previous Earlier HTML and echo Output?

有人知道为什么这个脚本在我尝试过的每台服务器上都能成功吗?尽管在头调用之前有早期输出,但我还是成功地重定向到了谷歌。

根据PHP文档,在输出后添加头失败并返回警告。然而,我在我的网络服务器上看到了不一致的行为。我一直在使用类似的方法来完成一些事情,除了一个随机停止工作的情况外,它运行得很好。

<?php
    echo "lol"; 
?>
<html>
<?php
    header("Location: http://www.google.com");
    exit();
?>

那怎么回事?PHP的最新版本现在允许这样做吗?

我的php版本是Ubuntu 14.04 x64 上的php 5.5.9-1ubuntu4.14

Output_buffering已在我的服务器上启用,这允许进行一些设置:

; Output buffering is a mechanism for controlling how much output data
; (excluding headers and cookies) PHP should keep internally before pushing that
; data to the client. If your application's output exceeds this setting, PHP
; will send that data in chunks of roughly the size you specify.
; Turning on this setting and managing its maximum buffer size can yield some
; interesting side-effects depending on your application and web server.
; You may be able to send headers and cookies after you've already sent output
; through print or echo. You also may see performance benefits if your server is
; emitting less packets due to buffered output versus PHP streaming the output
; as it gets it. On production servers, 4096 bytes is a good setting for performance
; reasons.
; Note: Output buffering can also be controlled via Output Buffering Control
;   functions.
; Possible Values:
;   On = Enabled and buffer is unlimited. (Use with caution)
;   Off = Disabled
;   Integer = Enables the buffer and sets its maximum size in bytes.
; Note: This directive is hardcoded to Off for the CLI SAPI
; Default Value: Off
; Development Value: 4096
; Production Value: 4096
; http://php.net/output-buffering
output_buffering = 4096

啊,php。