Headers and Output

Hello!

After weeks of torment, I’ve now finally become a pro at getting rid of those pesky blank lines and output before redirecting using “header”. :rofl:

Could someone please explain to me the rationale behind why we have to watch for things sent to the screen before using a header?

Thanks so much…

-Eric

Because headers are output by the server before any html. Headers tell the browser how much content there is in the page (basically its length) so the browser knows how much to expect, how to update its progress bar, when to disconnect etc.

All this information is sent to the clients browser before the html. Therefore if you output any html anywhere in your script you then trigger the webserver to output the headers. After they’ve been sent (along with some html) you cannot then change the headers because as the message says, they’ve already been sent.

Thats why its best to do your php in a seperate file to your html. That way you remain in control of whats happening in your php code without becoming confused. You can then generate all of your content and simply merge it into a html template at the very last moment AFTER you’ve taken care of any headers you need to send first.

Alternatively just lookup ob_start on php.net which turns on output buffering. This means you catch anything output from php to the webserver and store it instead meaning that you can adjust headers before then turning off output buffering.

Thank you for a very clear response!

-Eric

No worries, glad to have helped.