Not getting error for headers

Hi,
I’m new to PHP. Currently I’m learning on output buffering. Everyone says that there would an error if there is an output before header() function call. This is same with session() function. Currently I’m not getting this error (Cannot modify header information…) while practicing on localhost. I’m not sure what is problem. I’ve not changed my ‘php.ini’. I’m using PHP 5.4.3 and Appache 2.2.22. I want to use buffer functions to buffer my output before I can send it to browser. For practice I want to see this error and then remove this error using output buffering.

Can anyone please let me know how I would get this header errors? The example that I’ve used is available in php.net. That is:

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.google.com/');
?> 

Thanks,

Sounds like you have output buffering turned on in your php.ini file (http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering).

That is weird.

I checked the PHP Manual and also forced error_reporting(-1); and ini_set(‘display_errors’,1); and the elusive Cannot modify header information… never appeared ?

I reckon this is a job for @Salathe; or another PHP Guru.

I’ve solved the mystery! What you get is a warning:

PHP Warning: Cannot modify header information - headers already sent by (output started at…

warnings don’t kill the script and if you are using a production server that does not display warnings you don’t see it. I got the quoted message from my php error log.

Hi,
Output buffering is off in php.ini.

Hi,
I’ve checked php error log page. There is no such warning or error. I’m using default php.ini.

You probably have output_buffering turned on, with the <html> text not being long enough to cause the buffer to be flushed. Also try with a different header (e.g. Content-Type: text/plain) since the warning might be being printed but not seen due to the redirect.

Could you show us a phpinfo() page? The term “default php.ini” doesn’t really help anyone, there are many defaults. (:

Hi,
I’m sorry to everyone. I saw phpinfo page now and found that output_buffering is turned on. I turned it off and checked my code. It is working fine. Initially I was looking at php.ini file. I got confused with two declarations in php.ini for output_buffering. At first place it is turned off and at after few lines it is turned on. Those declarations are:

//at one location lines are

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

// at another location in php.ini the lines are

; 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

After looking at phpinfo I came to know output_buffering is turned on. Issue is resolved now. Sorry for bothering on this. I think in earlier version it was turned off by default and in current versions (PHP 5.4.3) it is turned on by default.

Thanks,

No need to apologise, it is a common enough issue (having different INI settings that what you think are being used).

[ot]An aside about “default” values.

The “default” value (used when no INI file overrides the setting) has always been Off. This is what we mean in the manual when we say “default” value. Up until PHP 5.3.0 when we used to ship “dist” and “recommended” sample php.ini files, the values were Off and 4096 respectively. As of PHP 5.3.0, we ship “development” and “production” sample php.ini files, both having value of 4096. Of course, those are only sample files and the actual php.ini that gets installed (if any) also depends on your method of installation; e.g. many Linux distributions use their own customised php.ini when installing PHP.[/ot]