2016-03-26 10 views
0

私は試した各サーバーでこのスクリプトがなぜ成功するのか誰にも分かりますか?ヘッダーコールの前に以前の出力があったにもかかわらず、Googleにリダイレクトされました。PHPヘッダーは以前のHTMLとエコー出力にもかかわらず作業をリダイレクトしますか?

PHP documentationによれば、出力後にヘッダーを追加すると失敗し、警告が返されることが記載されています。しかし、私は自分のWebサーバーで一貫性のない動作を見ています。私はいくつかのことを達成するために同様のアプローチを使用してきましたが、ランダムに動作を停止した1つのケースを除いてうまくいきました。

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

PHPの最近のバージョンでこれが可能ですか?

私のPHPのバージョンは、Ubuntuの14.04 x64の上でPHP 5.5.9-1ubuntu4.14ある

+0

ほとんどの場合、PHPはいくつかのキャッシング(出力バッファ)を行いますが、ときどき動作します;) – Miro

+0

PHPはすべての出力をデフォルトで保留して、必要なときにヘッダーを挿入できると便利です。私は本当の理由を知りたいのですが。 – OwN

+0

出力を「ホールドオフ」したい場合は、そのための機能があります:http://php.net/manual/en/book.outcontrol.php。一方、並行出力は、多くの用途で非常に重要です。 – Miro

答えて

0
として設定によって説明この一部を可能に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。

+0

良い副作用の1つは、output_bufferingを有効にすると、少なくとも私のサーバで7.0.4、lolというスクリプトをより高速にすることです。 –

関連する問題