Godless information headers. How to find and overcome BOM - an unpleasant error in WordPress. How does the Cannot modify header information — headers already sent by error occur?

People constantly come to me with this error and ask: " Where is the mistake?". Over the entire period of time I have received about a few letters like this 500 , not less. It's time to finish with the "" error. In this article I will talk about the reasons for this error, as well as how to solve it.

If we translate this error into Russian, you get something like this: " Can't change the header because they've already been sent"What is this" headers"? Let's figure it out.

When the server returns a response to the client other than the body (for example, HTML code pages), there are also headings. They contain the server response code, cookie, encoding and many other service parameters. Can PHP script send a title? Of course it can. There is a function for this header().

This function, for example, is constantly used when. Also this function regularly used for .

Also, headers are modified upon sending cookie and at the start of the session (function session_start()).

And now about why the error still occurs? The server always gives the headers to the server first, and then the body. If the server has already returned the headers, then the body goes, and then it encounters some session_start(). It turns out that the unfortunate programmer forgot to send the headers before the start of the body, and now wants to catch up with the train that has already left.

Here is the code with the error "":



?>

Of course, such nonsense PHP doesn't forgive. And it should have been written like this:

session_start(); // Let's start the session
?>

This script will not cause any errors, because all headers are sent first, and only then the server response body is generated.

Another example of code with an error:

echo "Hello!"; // Print something
session_start(); // Let's start the session
?>

The same thing, for some reason the body (or a piece of it) is displayed first, and then they remembered that they also need to modify the headers.

What is the correct way to rewrite this code, think for yourself.

Another example:




exit;
?>

When the author of such code fails, he is surprised by this error and says: “It’s a very strange coincidence, when the operation is successful, everything is fine, and when there is some error, they tell me Cannot modify header information - headers already sent.” Not literally, but that's the point.

The problem is the same, and the correct way to write it is:

$error = true; // Were there any errors?
if ($error) echo "An error occurred";
else header("Location: ".$_SERVER["HTTP_REFERER"]); // Redirect back
exit;
?>

There are also subtle errors:

header("Location: ".$_SERVER["HTTP_REFERER"]); // Redirect back
exit;
?>

The error in this code occurs due to a space, which is present before . Space is a normal character and is part of the response body. And when the server sees it, it concludes that there will be no more headers and it’s time to output the body.

There are also the following errors, which are of the same nature. Let's say there is a file a.html:

require_once "a.html";
header("Location: ".$_SERVER["HTTP_REFERER"]); // Redirect back
exit;
?>

And the person is sincerely surprised, where did the error come from if he did not output anything? Therefore, you don’t need to look specifically 1 file, and all the files that are included in it. And in those that are connected to those being connected, you also need to look so that there is no output.

And the last point, but more difficult. It turns out that sometimes this error occurs even with correct code. Then all it's a matter of encoding. Make sure the file encoding is " UTF-8 without BOM"and exactly" without BOM", not just " UTF-8". Because the BOM are the bytes that come at the very beginning of the file, and they are the output.

I really hope that this article will help solve absolutely all problems associated with the “” error, since I tried to highlight all the problems that arise. And then you need to turn your head on and think, what’s wrong in your code?

In this article we will look at what headers are needed for, without going into detail about which one is responsible for what. The roles of the most common headings will be described in subsequent articles.

All articles from the series:

  • What are Http headers? General theory.

HTTP stands for HyperText Transfer Protocol. A protocol is a set of rules by which different devices exchange data. It was created in the 1990s. Now it is used almost everywhere on the Internet. Everything you see in your browser window was obtained through this protocol. http headers are perhaps the main thing in communication between devices. They convey basic information about the connection being established and the information being transmitted through this connection.
Let's take a look at the communication diagram between the two devices. Let these devices be your computer and some server on the Internet:

As you can see, the browser sent an http request. It might look something like this:

GET /other-19 HTTP/1.1
Host: www.scriptsite.ru
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ru,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

In this case, the first line is the request line, all other lines are http headers that carry additional information about the request, about the client who is requesting this information, and about many other things.
In response to our request, the server can send the following headers:

Server: Apache/2.0.61 (Unix) mod_ssl/2.0.61 OpenSSL/0.9.8k mod_dp20/0.99.2 PHP/5.2.5 mod_python/3.3.1 Python/2.5.1 mod_ruby/1.2.6 Ruby/1.8.6 (2007-09-24)

X-Powered-By: PHP/5.2.5

Set-Cookie: PHPSESSID=ft47gokfee6amv3eda3k1p93s3; path=/

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Pragma: no-cache

Keep-Alive: timeout=10, max=1024

Connection: Keep-Alive

Transfer-Encoding: chunked

Content-Type: text/html

The first line is the status line. The remaining lines are headers. The diagram showed that the content of the page is also loaded. But this content is not typically displayed in header viewer plugins. And the content of the page is only a special case. According to the protocol, the page does not necessarily have to be transmitted. Instead, both a picture and sound file, and video. And all of them will have very different headings.

How to see http headers?

In order to see http headers, I recommend the following plugins for the firefox browser:

If you use Chrome browser, you can view all the information by clicking on the settings button - tools - developer tools. Networks tab.
For users opera browser I can’t give any advice because I’m not familiar with this browser. Once the plugins are installed and running, try refreshing the page. You will immediately see huge lists of requests and responses through which your browser communicated with the server.

Http headers and access to them in php

If you are a PHP developer, you can access request headers using the getallheaders() function. To understand how it works, let’s run the following code:

And we get a printout of the header array.

But more often they are accessed through the global variable $_SERVER. For almost everyone http header there is a similar name for the element in this variable, formed according to the HTTP_header_name principle. So for the same ‘User_Agent’ there is a variable $_SERVER[‘HTTP_USER_AGENT’];

To get the headers that the server is going to send to the user, the headers_list() function is used. As a rule, the server composes the missing required headers at the end of all scripts. Therefore, this array will contain the headers either those that the server created before the script started executing (and they will not be changed), or those that we set manually. They can be set manually using the function header("header text");
Let's run the following code:

We will see a printout of the headers ready to be sent at the time the function is called:

The first header was set automatically, and it carries the name of the server on which the script is running. The second one was installed manually by us. If the browser needed the "Fruit" header, it would take it from the server's http response and use it. But since our browser doesn’t need it, it simply ignores the line it doesn’t understand.

HTTP request structure

Our request looks like this:

The first line in it, as mentioned earlier, is the query line. It consists of three parts:

  • method(method) - indicates what type of request. The most common methods: GET, POST, HEAD. They will be written about in the next paragraph.
  • path(path) - usually this is the part of the URL that comes after the domain. For example, if you enter http://www.scriptsite.ru/about/ into the address bar, the path value will be /about/.
  • protocol(protocol) — the protocol used. Typically consists of "HTTP" and the protocol version. Typically, modern browsers use version 1.1

Next come the headers in the form of strings of the “Name: value” format.
By the way, cookie data is also transmitted in this request as one of the headers. Most of these lines are optional. The query can be reduced to just two lines:

GET /article/show/4/ HTTP/1.1

Host: scriptsite.ru

Request Methods

GET

A get request is typically used to request a document and pass some parameters.
This is the main method used to get HTML pages, images, CSS and JavaScript files, etc.
Due to the fact that the parameters can be anything, and the server has no restrictions on how they can be processed, the data request method is often used to transfer information. For example, we will have a form like this

In this case, these parameters will be visible in address bar browser.

POST

Post is the method used to send data to the server. Although you can send data to the server using the GET method through the browser's address bar, in most cases it is preferable to use POST. Sending large amounts of data via GET is impractical. In addition, GET has some limitations that do not allow, for example, publishing this article on my website through just one browser line. POST requests most often used to submit web forms. Let's modify the form from the previous example to give it a POST method

It's amazing how a small mistake can make your WordPress site completely inoperable. We're talking, of course, about the famous WordPress warning error Warning: cannot modify header information — headers already sent by pluggable.php (cannot change header information). If you are one of those who are facing this error, then you have come to the right place. In this, we will discuss the reason why this error appears in the first place and look at the solutions that will solve the problem permanently.

What you need

Before you start, make sure you have the following:

  • Access to your hosting control panel or FTP access

How does the Cannot modify header information — headers already sent by error occur?

Let's look at an example of this error to better understand the reasons. The error usually appears in this form:

Warning: Cannot modify header information - headers already sent by (output started at /public_html/wp-content/plugins/my-plugin/my-function.php:#) in /public_html/wp-includes/pluggable.php on line #

As you can see, the error mentions two files. The first file (in our case: my-function.php posted in /public_html//wp-content/plugins/my-plugin/) at the head of the suspects. This is our custom code that is designed to modify the core functionality provided by WordPress. The core functionality is in the file pluggable.php(WordPress core file, unchanged for any WordPress installations). In other words, the problem is in the first file, which prevents the second file from executing properly.

The main cause of the error is unnecessary spaces in the first file. These are spaces at the top or bottom of the file, unnecessary spaces anywhere in the file, or even spaces in PHP tags . By the way, because programmers can (and usually do) mistakenly insert extra spaces into their code, this error occurs more often than you might expect. The # line provided in the error message refers to the location of the problem - this will help resolve the problem faster and without fuss.

Fixing the error cannot modify header information - headers already sent by

Now that you know what's causing the error, you can move on to fixing it. We will show you two options to fix the problem, which you can try individually or in turn if individually does not help.

Option 1 – Editing the faulty file

The first solution to the error Warning: cannot modify header information– manual correction of a file with an error. You already have it in stock necessary information, to find the problem in the error message itself (remember, this is the first file in the message). All you need to do is open this file via FTP using a client like FileZilla or through a file manager.

Essentially, all that needs to be taken care of here is removing extra spaces/blank lines from the file. A good place to start is the # line mentioned in the error message. From here, you can continue parsing the rest of the file, looking for other unnecessary spaces or empty lines, all the way to the end of the document.

Make sure you spell the beginning and ending words correctly. PHP tags. There should be no space before or after the tag , as well as tag ?> . Also, the last line of code should not end with a space or an extra line feed.

In the screenshot below you can see the file wp-config.php, which has spaces before the first PHP tag.


CLUE: Many text editors can automatically remove unnecessary spaces. For example, to remove extra spaces in the Atom editor, highlight all the code and go to P ackages -> Whitespace -> Remove Trailing Whitespace.

Option 2 – Replace the faulty file

Of course, editing a whole series of files with errors can be difficult. The files could be related to a plugin or theme that you just installed on your site or could even be WordPress core files.

If the error is indeed caused by a plugin or theme, all you need to do is reinstall it. This action helps in most cases. On the other hand, if the WordPress core file is the cause of the error, the best solution is to take a clean copy of WordPress and replace the file with the error in your installation with the same one in the correct version. This will ensure that the faulty file is restored to the initial state, while the rest of your WordPress site installation remains intact. Now, just reload the page and make sure the error is fixed.

In conclusion

Whether you pasted a piece of code into a file, added a new plugin/theme, or wrote the code manually, there is a risk of extra spaces appearing in the file. These Seemingly Innocent Spaces Can Turn into a WordPress Error Warning: cannot modify header information — headers already sent by.

In this guide, we looked at how to fix such errors, and now your site is working as expected again. More WordPress tutorials can be found here.

Author

Elena has professional technical education in the field information technologies and experience in programming in different languages ​​for different platforms and systems. She has devoted more than 10 years to the web industry, working with different CMS, such as: Drupal, Joomla, Magento and of course the most popular content management system these days - WordPress. Her articles are always technically verified and accurate, be it a review for WordPress or instructions for setting up your VPS server.

One day, when I went to my blog, I was surprised to find an incomprehensible error, something like:

Warning: Cannot modify header information — headers already sent by (output started at /xxxxxxxx/wp-config.php:1)

Moreover, there is no way to enter the admin panel. I immediately went to check what was wrong with the wp-config.php file. Everything was in place, the database passwords were correct. I thought it was hacked again)) But again, no signs of vandalism were noticed on the FTP. The strangest thing (this ultimately completely confused me) was that only the link to the site without www did not work, or vice versa (I don’t remember exactly). I started contacting the hoster, looking at the settings in the domain admin panel - in general, a lot of things.

But it turned out to be much simpler - at the beginning of the config file there was a certain BOM— marker (signature) for UTF-8 files. That is why the above error popped up. To prevent this from happening to you, first of all you need to use code editors that either do not put this signature at all, or before saving the file they clarify whether it is needed.

In some text editors, you may find "Include Unicode Signature (BOM)", "Add Byte Order Mark" or similar checkboxes in the settings. Otherwise, without being able to disable an unnecessary option in a particular program, it is not recommended to use it. On specialized forums you can find a list of good text editors, This - Notepad2, PSPad, UnicEdit, Notepad++. In general, a lot is written about the latter; it is quite a powerful tool. Somehow, by chance, I had an alternative editor available on my computer - Akelpad— I use it for similar tasks.

Another point worth noting is that an error with BOM can occur not only in the wp-config.php file. Moreover, if the option to display warnings is disabled, you will not see where the problem has crept in at all. In such cases (and all others) I would recommend using a simple script for searching files with BOM. Yuri Belotitsky should be thanked for the development.

Using the script is very simple.

  1. required file
  2. You pour it on FTP server to the root directory. If WordPress is not installed in the root of the site (but in the blog folder, for example), then the script must be placed in the directory where WordPress is located, and run from there.
  3. Launching is very simple - just type in the address bar of your browser the link http://your.site/find_bom.php

As a result, you will get a list of files that are faulty. By the way, to speed up the work, the script checks only those directories where users usually upload files - root, /wp-content/themes and /wp-content/plugins.

That's basically it. How difficult it was to solve such a simple problem. I hope I helped you a little with my experience, and now when the corresponding warning appears, you will know what to do :) If you can’t fix one or another file from the BOM, you can simply upload a new one from the WordPress distribution.

P.S. A suitable site for newlyweds is organizing banquets and resolving all issues related to the wedding.

In this article we will look at the main causes and solutions for the error “It is impossible to change the headers because they have already been sent” ("Cannot modify header information - headers already sent by").

What does this error mean?

To understand the reasons for the error, you must first understand what these “headers” are.

Let's not go deep into theory. Let’s just say that before any user opens a web page, these same “headers” are sent to him, which contain the encoding, site language, server data and other service information. It’s also worth adding separately that cookies and session are also sent in the headers.

What commands cause this error?

Error "Cannot modify header information - headers already sent by" can call PHP commands such as header , setcookie and others related to the operation of cookies or sessions.

Reasons and solutions for the error.

The most common mistake happens due to lack of experience. We've already figured out that headers are sent before the page itself starts loading.

But programmers, especially beginners, simply forget or don’t even know this. And first they try to display something on the page - most often using the echo command, and then they set cookies, send headers, etc. Which leads exactly to this error.

Here is an example of code that would result in this error:

Here's the correct option:

That is, firstly, you cannot display anything before sending the headers!

It's not always obvious, but there is an error with a slight difference. This is when your PHP document begins with spaces or empty lines, which means that these lines are displayed in the browser.

This can be very difficult to monitor, since, for example, Windows notepad can add a Byte Order Mark at the beginning, without warning us in any way or even showing this symbol. In this case, you should open the document using other editors and check.

Here is an example of setting headers incorrectly:

That is, secondly, before

You should be especially careful if you use the include command, essentially it combines all the files and makes one resulting one, and if you first included the site header (slider, menu, etc.) and then try to send headers in the main file, then you Of course you will get this error.

Here is an example of such incorrect code: