问题
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.
回答1:
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.
回答2:
you may have white space before at the top of page
This will lead that error "HEADER ALREADY SEND "
No idea about ob_start()
回答3:
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.
来源:https://stackoverflow.com/questions/4168009/necessity-of-prefixing-all-php-pages-with-ob-start