Ashley Sheridan​.co.uk

Fixing the Headers Already Sent Error

Posted on

This is probably the single most frequent error I've seen come up as part of a question on mailing lists and forums, usually when one moves a script from a more lenient server to one that is more strict in its error reporting.

The issue is caused by some call to header() after some output is sent to the browser. What can be sometimes difficult to understand for programmers new to PHP is that HTML is embedded in a PHP script, and not the other way around. Understanding this is the first step to diagnosing, and then fixing, the problem. What this means is, that any HTML content that occurs before the first <?php tag is treated by the web server (Apache, IIS, etc) as output to be sent straight to the users browser. Then, once it reaches PHP code, it passes this across to the PHP interpreter to handle.

There is a quick fix to the issue, one that comes up many times as an answer, and that is to turn on object buffering. While this will work in the majority of cases, it does introduce a large memory overhead, which could mean that the server takes longer to respond, or that fewer visitors can access your website at one time. To turn on the output buffering, either set the output_buffering value to some suitably large value (the number of bytes in your output) in your php.ini file so that output is automatically buffered to this point before output is sent to the browser or use these commands in your PHP code:

ob_start(); // the rest of your code goes here ob_end_flush();

The first line turns on the output buffering, and the second is called when you want to send that data to the browser.

Now, I warned that this has a major caveat in that it essentially buffers your entire web page into memory before sending it to the browser. Calculate the size of your webpage (the HTML only). Now imagine that amount multiplied by the maxiumum number of concurrent users, ontop of the regular memory usage that your script will consume, and you may notice a problem. Once the number of visitors goes beyond a certain amount, you'll start to have problems.

The second solution to the headers sent problem is to actually address the real root of the issue and stop output before you try to issue some sort of header() call.

Firstly, make sure that <?php is the very first line in your script and that there are no calls to echo, print etc in between that line and the first header() call. If you can't make either of these happen, then you might need to rethink the actual logic of your code.

If you've done that and are still having problems, make sure that any files pulled in by include or request are free of any output. Even a single space before the <?php part will trigger output to be sent to the browser.

The last cause could be because of something called a BOM which can sometimes be added by certain text editors. Try to open your PHP file and save it without a BOM, the exact option for this may vary between editors.

Conclusion

In conclusion, this is definitely an issue that can be resolved quite easily. The only complexity could come from bad logic in your code, for example where you try to use a header redirect after sending a message to the user. If this is something that you want to try and do, it might be better to look at a client-side redirect, such as a <meta> refresh, or a some JavaScript to redirect the browser.