sepoto Posted September 10, 2011 Share Posted September 10, 2011 I had some code and recently I moved over to a new server which happens to be running Debian 5.0 Lenny. I had some code involving page redirection that stopped working and I had to add ob_start(); to the top of the .php files in question to get them working again. I read some documentation for ob_start(); but I have to admit it did not make sense to me at all. Why am I unable to send my headers unless ob_start(); has been invoked first? Is there a stack someplace being created? If so is it a stack only for header information? What exactly is ob_start(); doing? Thank you! 1 Link to comment Share on other sites More sharing options...
boen_robot Posted September 10, 2011 Share Posted September 10, 2011 Normally, the output of PHP stats as soon as your first "echo" and the like or first non-PHP content. Once output starts, you can't send headers, which among other things include cookies and redirects (basically, anything that is not "output", you can count as a "header"). It seems your old server didn't immediatly started sending output, but instead waited for your script to finish. This allowed your script to send headers even after your first echo. Why exactly did it do that is not certain. It is typically due to some kind of output altering script that runs transparently after yours - because the output might be altered, the web server would always wait for those scripts to run before it sends the output. Where does ob_start() fit into this? ob_start() holds out PHP output until you either call ob_flush() or the script ends. In other words, you again hold out output. It's just that this time you're doing it on the PHP level rather than the web server level. For performance's sake, it would be best if you could instead rewrite your script in such a way that the redirect detection is done before any output. 2 Link to comment Share on other sites More sharing options...
sepoto Posted September 10, 2011 Author Share Posted September 10, 2011 That was a great explanation. Thank you for your reply. Link to comment Share on other sites More sharing options...
niche Posted October 9, 2013 Share Posted October 9, 2013 Oldie, but goodie Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now