Cannot modify header information – headers already sent

So what exactly is ‘modify header information’ anyway? Well, it’s quite a common problem and one that has no definitive answer because there are many different code reasons why it occurs. That said, if you understand why it’s happening you should be able to diagnose your issue more easily. I’ll give an explanation first and then go on to provide a fix for WordPress template files as these seem to be the most prevalent code examples that get hacked and chopped about.

Headers sent to your browser explained

So, this issue rears its ugly head when someone visits your website and requests a page. Your server duly sends the page but before the visitor’s browser has completed the rendering of that page on the screen, it gets told to redirect to another page. This is unacceptable and the browser doesn’t like this so you see the error “Cannot modify header information – headers already sent”. Take this code example:

<html>
<?php
header('Location: https://www.pcrepairmansblog.com/');
exit;
?>

It’s simple enough php code where the object is to redirect the user’s browser to another website. This will not work though because the <html> line has started to output the code to the browser already. Remember that header() must be called before any actual output is sent and this can be by standard HTML, via PHP or even via the presence of blank lines.

The blank lines header error

The presence of blank lines within PHP in a file can cause errors. Here’s another example:

/*Some file*/

<?php

header('Location: https://www.pcrepairmansblog.com/');

exit;

? >

Spot the mistake? It’s a common one but the extra space after the final question mark is actually output to the browser and can be the difference between the page working or not. Extra whitespace where it shouldn’t be is tricky for coding beginners to spot, so work logically through the code and try to structure it as cleanly as possible. Compare any modifications you have made to the original file and try the original again to see if that triggers the error. Typical problems include whitespace or new lines before the opening <?php or after the closing ?> which works in many cases but often causes this error. Try to code more cleanly and remember this can cause big problems later on.

If your code has more than one PHP block in it and they are directly after each other, remove any spaces in between them. Try to consolidate the PHP into one block if possible here too.

 

Modify headers error where session_start() is used

Here’s another scenario:

<html>

<?php session_start(); ?>

<head>

<title>What's wrong?</title>

</head>

</html>

So what is going wrong here? Well, the session_start() function attempts to send headers with the session cookie to the client. Unfortunately, PHP already sent headers when it wrote the title element to the ‘output stream’. To resolve this, you would need to move the session_start() code to the top, above the <html> line.

Often, the error indicates exactly where you should be looking in your code so look for php and html output around there.

Script encoding errors, UTF-8 and BOM

The Byte Order Mark (BOM) is a Unicode character used to signal the byte order (aka ‘Endianness’) of a text file or stream. Still with me? If you’re not, don’t worry, all you need to do is to try to make sure you don’t have any Byte Order Marks in your code as this messes up the headers too. There is, in my mind, little place for BOM on a WordPress installation. The Unicode standard permits BOM in UTF-8 but doesn’t recommend it.

Errors caused by the inclusion of BOM are generally because

  • You viewed the source in a bad text editor and saved it (hint use Notepad++ which is free and brilliant)
  • You used a poor FTP client (hint: use WinSCP, also free and brilliant)
  • You had the BOM in there originally (hint: don’t download files from dodgy sites).

The simple solution is to open up all the offending files in Notepad++ (or a similar good text editor) and swap the file format from Windows/Mac to Unix and turn off the BOM.

For advanced users, you can run this nifty ‘find’ code on the server to remove all BOM code. Use with caution as it can modify any file.

find . -type f -exec sed '1s/^\xEF\xBB\xBF//' -i.bak {} \; -exec rm {}.bak \;

If you prefer to tread cautiously, use this code to simply display those BOM files:

grep -rl $'\xEF\xBB\xBF' .

The code above cleverly looks in the first line only which is where we find the BOM byte sequence (the UTF-8 representation of this is 0xEF,0xBB,0xBF). This means it runs pretty quickly.

Code your way out in functions.php

Well, I said I would give you a solution that works for WordPress and here it is. Please note, I would thoroughly recommend attacking the other solutions above first but if you are at your wits end try adding this to your theme’s functions.php file:

add_action('init', 'do_output_buffer');
function do_output_buffer() {
        ob_start();
}

PHP will now not send any input to the browser until the page is fully loaded. This in turn allows your WordPress installation to redirect users as it sees fit.

Summary

Remember, what you are trying to find is basically two lots of output to your browser and this is often via HTML code and PHP code being run at the same time. Track it down and your error should be removed. Don’t underestimate the widespread duff code in 3rd party plugins too, disable these one by one and try again.

Hope this helps you to sort out your ‘headers already sent’ issues, please use the social links to recommend this page to others before they pull their hair out too 🙂