2014-10-16 79 views
0

我在某些輸出後放置了標題功能,但我仍然被髮送到指定的位置。在php.ini output_buffering的爲什麼PHP中的header()函數拋出一個錯誤?

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

設置都被註釋掉了

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

爲什麼我仍然被重定向到google.com?

+4

您是否在編輯php.ini後重新啓動服務器? – Maccath 2014-10-16 09:50:31

+0

可能重複的[PHP頭位置重定向不起作用 - 爲什麼?](http://stackoverflow.com/questions/2710079/php-header-location-redirect-doesnt-work-why) – 2014-10-16 09:51:47

+0

我沒有編輯任何東西,它總是有這種方式 – user1906399 2014-10-16 09:59:06

回答

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中,你會發現如上。查找關鍵字「output_buffering」整個php.ini文件,它應該是在某個地方。要檢查它的另一種方式,在任何php頁面中使用phpinfo();,然後運行該頁面,您會發現output_buffering爲'1'表示它處於On狀態。因此,通過這樣做,您可以終止或暫時停止它,您可以使用ob_end_flush();,如前所述。

+0

ini文件中output_buffer的配置總是存在,我沒有編輯它。 – user1906399 2014-10-16 10:32:47

+0

你在本地使用WAMP嗎? – 2014-10-16 10:37:40

+0

我使用xampp – user1906399 2014-10-16 10:42:39

相關問題