PHP标头重定向工作尽管早期的HTML和回声输出?

时间:2016-03-26 00:32:01

标签: php html redirect http-headers echo

有没有人知道为什么这个脚本在我试过的每台服务器上都能成功?尽管在标题调用之前已经提前输出,但我已成功重定向到Google。

根据PHP documentation,声明在输出失败后添加标题并返回警告。但是,我发现我的Web服务器上的行为不一致。我一直在使用类似的方法来完成某些事情,除了一个随机停止工作的情况之外,它工作得很好。

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

那么,这笔交易是什么?最新版本的PHP现在允许这个吗?

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

1 个答案:

答案 0 :(得分:1)

在我的服务器上启用了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。