My friends and family are under attack in Ukraine. Donate to protect them directly or help international organizations.

Sending headers

February 7th, 2008

There are many reasons to send headers in the middle of you PHP script. Sometimes you want to redirect the user based on some computed information or display different content types (mime-type). Have you ever had a problem with PHP complaining about "output already sent"?

To solve this problem, there is a workaround with ob_start/ob_flush. As pointed out in a past comment, you can put ob_start in the beginning of the script and ob_flush at the end, thus preventing any output before the script finishes executing.

A more neat solution (in my opinion) is to review your output approach. You should not send any output before you are absolutely sure about it (why would you sent kilobytes of HTML if you're going to redirect anyway?). My approach is to store output in a variable or a class and then send it when ready. I also try to limit html generation until after I processed my GET/POST requests.

Previous: FlexNativeMenu Tutorial & Issues Next: AIR Prerelease Tour