Necessity of prefixing all PHP pages with ob_start()

旧时模样 提交于 2019-12-11 02:40:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!