Necessity of prefixing all PHP pages with ob_start()
Asked Answered
C

3

2

Is it OK to have ob_start() in the beginning of all PHP pages?

If I don't do this, I get the "headers already sent" error.

Claribelclarice answered 12/11, 2010 at 18:47 Comment(5)
If those errors are being raised against your intention you may want to check for loose/premature output and stuff first.Possibility
yup, remember to open_end_clean it ob_end_flush but u should look for where the header come fromDel
@Del - I'm pretty sure you meant ob_end_clean, not open_end_clean. :)Targum
@Matt Huggins - soli, is 3am now ...Del
+1 to compensate for the downvote (why btw?) and because this is actually a nice topic about general application style.Contribution
I
2

It is "ok" but you should really fix your script to not output stuff prematurely and save the output for the end. But you can just change a setting in .htaccess or the php.ini the below is the for .htaccess:

php_value output_buffering On
php_value output_handler mb_output_handler

This would be preferred, in my opinion, instead of adding ob_start to the top of all your scripts.

However, you really should fix the problem at its core. IE: Save output for the end of the script.

Impasse answered 12/11, 2010 at 18:55 Comment(0)
L
1

you may have white space before at the top of page

This will lead that error "HEADER ALREADY SEND "

No idea about ob_start()

Landsknecht answered 12/11, 2010 at 19:18 Comment(0)
C
1

I would say it is okay to do this as a workaround or when simply quickly making your existing script work. In the long run however you should think about redesigning your application so things like this don't happen. In general you should try to completely separate application logic and the output.

Contribution answered 12/11, 2010 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.