Question

I have a few questions regarding headers and output buffering.

I know headers must be send before output or they will not work, and that output buffering stores all HTML into a buffer and sends it as one as opposed to PHP processes sent bits at a time.

So does this mean when output buffering is on, all content is collected into one variable and where ever the headers were defined in the script they will be placed at the top/first?

And if output buffering is off you have to declare headers before any output?

And also to use any output buffering functions such as ob_clean() you need output buffering to be on? as if output buffering was off, you couldn't clean, 'take back', anything as it would of already been sent?

Finally is output buffering turned on/off within php.ini? as my XAMPP local host server seems to have output buffering on and my VPS doesn't, meaning I need to go to my VPS php.ini?

Was it helpful?

Solution

So does this mean when output buffering is on, all content is collected into one variable and where ever the headers were defined in the script they will be placed at the top/first?

Most like that. There's no "PHP variable" that contains the content but you can access it using the ob_*() functions. For instance,ob_get_contents() to get the buffered content, ob_clean() to erase it, and so on.

Headers are sent just before the first content is sent. In fact you can override previously specified headers using the replace (the second argument) of the header() function.

And if output buffering is off you have to declare headers before any output?

Yes, otherwise you get a "Warning: Cannot modify header information - headers already sent...".

And also to use any output buffering functions such as ob_clean() you need output buffering to be on?

No, but you need to call ob_start() to start the buffering process.

Finally is output buffering turned on/off within php.ini?

Check the output_buffering directives.

meaning I need to go to my VPS php.ini?

The output buffering feature is configurable PHP_INI_PERDIR. You can use a .htaccess file or you can just add ob_start() at the beginning of your script.

OTHER TIPS

So does this mean when output buffering is on, all content is collected into 
one variable and where ever the headers were defined in the script they will be 
placed at the top/first?

Not quite, headers are sent regardless of buffering state, all other echo & print are buffered until you release them.

And if output buffering is off you have to declare headers before any output?

Yes this is correct.

The Automatic state of Output Buffering is controlled from the PHP.INI

The manual

Assuming your XAMMP is your development environment I suggest changing that to match your LIVE VPS environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top