8

What are the methods to turn on output buffering either within a PHP script or using and htaccess file?

I use the following method in an htaccess file in the root of my application:

php_value output_buffering On
php_value output_handler mb_output_handler

On one of my shared hosting accounts (linux hosting with PHP 5.2.x), the above yields a blank page. Tech support says they can't turn it on in the php.ini file but I can turn it on in my script...

ob_start() and ob_end_flush() also yields the same result. What can I do?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    If you get blank pages, look into the `error.log`. Did you set an [output encoding](http://php.net/manual/en/function.mb-output-handler.php) before activating the mb_output_handler? – mario Jan 06 '11 at 23:14
  • @mario, no, i didn't know i had to. how can i do that? –  Jan 06 '11 at 23:21
  • 2
    If you want this deleted, please flag it for moderator attention (as I just did). Kindly, stop rolling this back, it just puts nonsense on our front page :( – Tim Post Jan 22 '11 at 00:18

2 Answers2

7

Use ob_start() and ob_end_flush().

ob_start() at the start of the script before any output (not even an empty space).

When u want to output use ob_end_flush().

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Diablo
  • 3,378
  • 1
  • 22
  • 28
  • 2
    2nd time ive seen this today. You can turn on output buffering AFTER or BEFORE printing content. It makes no difference. http://ideone.com/URuKl – profitphp Jan 06 '11 at 23:17
  • Sure it makes a difference. If you print something without output buffering turned on, it will actually be output. – JW. Jan 22 '11 at 00:29
  • @klinky i was not trying to demonstrate ob, i was demonstrating that you can call it at any point, not just at the start before any output, as predrag.music stated. – profitphp Jan 22 '11 at 03:52
  • @jw it makes a difference, but its not going to break it and not work, which seems like what he was implying. I think people get it confused with calling the header function, because often people will use output buffer to make sure they can use the header function. – profitphp Jan 22 '11 at 03:58
2

Check your PHP.ini file and ensure the Output Buffer is enabled.

After that, you can use ob_start() whenever you want to begin buffering and ob_flush() to flush the buffer whenever you want to stop buffering.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
user509006
  • 107
  • 2
  • 9
  • 1
    If you use cPanel (most hosts do), there is an option in there to change the php.ini configurations. – user509006 Jan 06 '11 at 23:24
  • 1
    FYI Scott, I use hostmonster.com, which is shared hosting, and I can still edit my own php.ini file. – justinl Jan 06 '11 at 23:28
  • @justinl, i know what you mean, on most of my account i can do the same - not all however. –  Jan 06 '11 at 23:31
  • that's unfortunate :S I'm not sure why those other methods aren't working for you – justinl Jan 06 '11 at 23:33