为什么php中的header()函数没有抛出错误?

时间:2014-10-16 09:47:53

标签: php http-headers

我在输出之后放置了标题函数,但我仍然被发送到指定的位置。

 <!doctype html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <title>Document</title>

 </head>
 <body>
    hello
    <?php echo 'hello'; ?>
    <?php header('Location: http://www.google.com/'); ?>
 </body>
 </html>

php.ini中output_buffering的设置都已注释掉

; output_buffering
;   Default Value: Off
;   Development Value: 4096
;   Production Value: 4096

为什么我仍然被重定向到google.com?

1 个答案:

答案 0 :(得分:2)

 <?php ob_end_flush(); 
echo 'hello';
 header('Location: http://www.google.com/'); ?>

您可能在服务器上启用了输出缓冲:在脚本完成运行之前,输出缓冲不会输出任何内容。正如您所提到的,您已关闭,因此您需要重新启动服务器。这允许它在实际输出之前获取标题(因为它知道应首先发送标题)。

如果这是有道理的。
修改

; 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 = On

在php.ini中,您将找到上述内容..查找关键字&#39; output_buffering&#39;整个php.ini文件,它应该在某个地方..要以其他方式检查它,在任何php页面中使用phpinfo();并运行该页面,你会发现output_buffering为&#39 1&#39;意味着它开启..所以通过关闭你可以终止它或暂时停止它,你可以使用我前面提到的ob_end_flush();