OwN
OwN

Reputation: 1338

PHP Header Redirect Working Despite Previous Earlier HTML and echo Output?

Does anyone know why this script succeeds for me on each server I've tried? I am successfully redirected to Google despite earlier output before the header call.

According to the PHP documentation, it is stated that adding headers after output fails and returns a warning. However, I am seeing inconsistent behavior on my web servers. I've been using similar approaches to accomplish some things, and it has worked just fine except for one case where it randomly stopped working.

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

So, what's the deal? Do recent versions of PHP now allow this?

My php version is PHP 5.5.9-1ubuntu4.14 on Ubuntu 14.04 x64

Upvotes: 1

Views: 207

Answers (1)

OwN
OwN

Reputation: 1338

Output_buffering was enabled on my server which allows for some of this as explained by the setting:

; 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.

Upvotes: 1

Related Questions