PHP Output Buffering
Asked Answered
T

2

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?

Tuckie answered 6/1, 2011 at 23:9 Comment(3)
If you get blank pages, look into the error.log. Did you set an output encoding before activating the mb_output_handler?Ark
@mario, no, i didn't know i had to. how can i do that?Tuckie
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 :(Elsie
D
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().

Drogheda answered 6/1, 2011 at 23:14 Comment(4)
2nd time ive seen this today. You can turn on output buffering AFTER or BEFORE printing content. It makes no difference. ideone.com/URuKlAirbrush
Sure it makes a difference. If you print something without output buffering turned on, it will actually be output.Gord
@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.Airbrush
@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.Airbrush
B
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.

Baptist answered 6/1, 2011 at 23:19 Comment(4)
If you use cPanel (most hosts do), there is an option in there to change the php.ini configurations.Baptist
FYI Scott, I use hostmonster.com, which is shared hosting, and I can still edit my own php.ini file.Haimes
@justinl, i know what you mean, on most of my account i can do the same - not all however.Tuckie
that's unfortunate :S I'm not sure why those other methods aren't working for youHaimes

© 2022 - 2024 — McMap. All rights reserved.