Jump to content
sepoto

Ob_Start();

Recommended Posts

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!

  • Like 1

Share this post


Link to post
Share on other sites

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.

  • Like 2

Share this post


Link to post
Share on other sites

That was a great explanation. Thank you for your reply.

Share this post


Link to post
Share on other sites

Oldie, but goodie

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...