2016-03-26 5 views
0

Weiß jemand, warum dieses Skript für mich auf jedem Server, den ich ausprobiert habe, erfolgreich ist? Ich wurde trotz der früheren Ausgabe vor dem Header-Aufruf erfolgreich an Google weitergeleitet.PHP Header Redirect Arbeiten trotz früherer HTML und Echo-Ausgabe?

Gemäß der PHP documentation wird angegeben, dass das Hinzufügen von Kopfzeilen nach der Ausgabe fehlschlägt und eine Warnung zurückgibt. Auf meinen Webservern sehe ich jedoch ein inkonsistentes Verhalten. Ich habe ähnliche Ansätze verwendet, um einige Dinge zu erreichen, und es hat gut geklappt, außer in einem Fall, in dem es nicht mehr funktionierte.

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

Also, was ist das Geschäft? Erlauben neuere Versionen von PHP dies?

Meine PHP-Version ist PHP 5.5.9-1ubuntu4.14 auf Ubuntu 14.04 x64

+0

Wahrscheinlich spuckt PHP etwas Caching (Ausgabepuffer), deshalb funktioniert es manchmal;) – Miro

+0

Es wäre schön, wenn PHP auf allen Ausgaben standardmäßig deaktiviert wäre, so dass Sie Header jederzeit einfügen können. Ich würde gerne den wahren Grund wissen. – OwN

+0

Wenn Sie die Ausgabe "halten" wollen, gibt es dafür Funktionen: http://php.net/manual/en/book.outcontrol.php. Auf der anderen Seite ist die gleichzeitige Ausgabe sehr wichtig für viele Anwendungsfälle ... – Miro

Antwort

0

output_buffering wurde auf meinem Server aktiviert, die durch die Einstellung wie erklärt einige dieser erlaubt:

; 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 

Ugh , php.

+0

Ein schöner Nebeneffekt ist, dass output_buffering das Skript schneller macht, zumindest auf meinem Server mit 7.0.4, lol. –