How to resolve the 'headers already sent' error?
This is one of the most common errors. You often get it when your using session_start()
Headers are sent automatically when any output is sent to the user. You send output with print
(and some other functions) as well as having anything outside php tags. This includes blank lines spaces, tabs and all other characters. You must send the header BEFORE the output. You must watch out not to have any blank lines before the <?php tag and after the ?> tag
<?php // There should be no spaces before tag.
// A new line which shouldn't be here. This is important especially the file is included to another file.
Coding in such a way might be difficult, so you might ask if there is a way to wait with sending the output until the end of script execution. This is where output buffering (ob for short) comes in to play. If you start ob it will save all output and headers into memory instead of sending it. You can send all output at once with the headers.
ob_start(); // Send the output.
header('Content-type: text/plain'); // An example header.
ob_end_flush(); // Send the output.
You can try commenting out the ob_start();
-mentioned but not OB functions
Good to read:
If you spot an error (or have a suggestion) PM me. I'll fix it as fast as I can.