Fugitive information headers. How to find and combat the BOM is a nasty mistake in WordPress. How Cannot modify header information - headers already sent by error occurs

Today we decided to talk about what the message means. "Warning: Cannot modify header information - headers already sent by (output started at / home / ..."that appeared on the site page instead of its main content.
As it turned out, the network has written enough on this topic, but there is no generalized instruction on what all this means and how to get rid of it.
We decided to add a few drops to the huge sea of \u200b\u200binformation on this topic, since we faced this problem personally.

Some time ago, we carried out the transfer of several client sites from one hosting to another.
Everything went fine, the sites were available, but when I tried to go to the admin. panel, after entering the login and password, a white page appeared instead of the control panel.
We checked it on other sites - the same thing.
To find out possible reasons, we have enabled the display of errors.
To do this, you need to use FTP to edit the .htaccess file located in the root of the site, adding the line to it:

Php_flag display_errors on

After that, when entering the admin panel, several messages appeared like "Warning: Cannot modify header information - headers already sent by (output started at /home/.../functions.php:1552) in /home/.../public_html /wp-login.php on line 362 ", etc.

As a result of the searches, information was found that this message notifies that the header information cannot be changed, because the headers (information about them) had already been sent earlier and then in brackets it was indicated which lines in which files this was done.


Headings (Headers) Is the service information of the server on which the site is located. Before the browser displays the content of the site, it receives headers from the server, where various data are indicated: whether the page is cached, its encoding, page content type, and others. In site content management systems, headers are formed by functions located in various system files.
Required condition - headers must be sent before the main content (content) of the page.


If the content of the site is transmitted before the headers, then a situation arises, about which we are warned by the message "Warning: Cannot modify header information - headers already sent by ..."

In what situations can this occur? As already mentioned, in modern CMS, headers are the result of one or more functions. The function itself is a piece of code enclosed between the initial and final ?> tags.

Anything outside of these tags is considered page content.
Thus, if at the beginning of the page there are functions that result in the sent headers, and we receive the message "Warning: Cannot modify header information ...", then it turns out that some information related to the page content is sent from the server before the headers ...

What is this information and how to find it. Most often these are spaces and blank lines.

A space or an empty string is interpreted as characters in the main content of the page, so it turns out that some of the main content is before the headers and is sent to the browser first.

You need to download the files indicated in the "Warning: Cannot modify header information ..." messages to your local computer, open them in a code editor (I use NotePad ++) and carefully check for empty lines and spaces:

However, there is one important feature that can significantly increase the time to find a solution.
The file may not contain empty lines and spaces, but if it was saved in UTF-8 encoding, then an extraneous character at the very beginning of the document may be inserted by the editor in which the file was created. This character is a UTF-8 identifier equal to a zero-width space, which may not be displayed at all in the editor, but on the server it will be perceived as the main content and displayed before the headers.

In order to get rid of this identifier, you need to resave the downloaded files in the format UTF-8 without BOM (UTF-8 without BOM).

NotePad ++ does an excellent job with this task.

After these actions and updating the files on the server, the message should disappear and the site will work as usual.

This error is not difficult to fix.
Often the same message appears at the start of sessions, in a slightly different wording:
Warning: Cannot send session cookie - headers already sent
Warning: Cannot send session cache limiter - headers already sent

Byte order mark
Sometimes you have checked EVERYTHING - there is nothing anywhere. Change the editor. Check your file in another program. For example, Windows Notepad when using Unicode encoding, it adds the Byte Order Mark to the beginning of your file, without informing you in any way. Open the script in another editor and remove any extraneous characters. And change Notepad to another editor.
Or save in UTF-8 encoding without BOM

Numerous questions on the forum make me do here important note:
This error does not appear because you have "something written above" in your script. And from the fact that PHP displays something in the browser. It doesn't have to be code. It could be error message... can be a space or html tag. Yes Yes. For the most talented: it comes about any character sent to the browser, not just those that the browser displays to an inexperienced user. Have HTML pages there is source text. And it is he who is the result pHP work script, and not pretty letters with pictures, as a very large number of people think.

In this article we will look at what headers are for, without going into details, which one is responsible for what. A description of the roles of the most common headings will be given in the following articles.

All articles from the cycle:

  • What are Http headers. General theory.

HTTP stands for HyperText Transfer Protocol (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 that you see in the browser window was received through this protocol. http headers are perhaps the main thing in communication between devices. They convey basic information about the established connection and the information transmitted through this connection.
Let's take a look at the communication diagram of 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 \u003d 0.9, * / *; q \u003d 0.8
Accept-Language: ru, en-us; q \u003d 0.7, en; q \u003d 0.3
Accept-Encoding: gzip, deflate
Accept-Charset: windows-1251, utf-8; q \u003d 0.7, *; q \u003d 0.7
Keep-Alive: 300
Connection: keep-alive

In this case, the first line is a request line, all other lines are http-headers that carry additional information about the request, about the client who requests this information, 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 \u003d ft47gokfee6amv3eda3k1p93s3; path \u003d /

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

Pragma: no-cache

Keep-Alive: timeout \u003d 10, max \u003d 1024

Connection: Keep-Alive

Transfer-Encoding: chunked

Content-Type: text / html

The first line is the status line. The rest of the lines are headers. It was shown in the diagram that the page content is also loaded. But this content is usually not customary to display in plugins that view headers. And the content of the page is just a special case. By the protocol, the page does not have to be transmitted. Instead of it, both a picture and sound file, and video. And all of them will have very different titles.

How can I see the http headers?

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

If you are using chrome browser, you can view all the information by clicking on the settings button - tools - developer tools. Networks tab.
Users opera browser I can not advise anything, since I am not friends with this browser. With plugins 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 accessing them in php

If you are a php developer, you can access the request headers using the getallheaders () function. To understand its work, let's execute the following code:

And we get a printout of the header array.

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

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

We will see a printout of 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 is manually installed by us. If the browser needed a Fruit header, it would take it from the server's http response and use it. But since our browser does not need it, it simply ignores the line that it does not understand.

HTTP request structure

Our request looks like this:

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

  • method (method) - indicates what kind of request. The most common methods are GET, POST, HEAD. They will be discussed in the next section.
  • path (path) - This is usually the portion of the URL after the domain. For example, if you type in address bar http://www.scriptsite.ru/about/, the path value will be / about /.
  • protocol (protocol) - the protocol used. Typically composed of "HTTP" and the protocol version. Usually, in modern browsers version 1.1 is used

Then there are headers in the form of strings of the "Name: value" format.
By the way, data about cookies is also transmitted in this request as one of the headers. Most of these lines are optional. The query can be reduced to two lines altogether:

GET / article / show / 4 / HTTP / 1.1

Host: scriptsite.ru

Request Methods

GET

get request is usually used to request a document passing 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 any, and on the server there are no restrictions on how to process them, often the method for requesting data is used to transfer information. For example, we will have such a form

In this case, these parameters will be visible in the address bar of the 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 via the browser address bar, in most cases POST is preferred. Sending large amounts of data via GET is impractical. In addition, GET has some limitations that do not allow, for example, to publish this article on my site through just one browser line. POST requests are most often used to submit web forms. Let's modify the form from the previous example by giving it the POST method

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

What is required

Before starting, make sure you have the following:

  • Access to your hosting control panel or FTP access

How Cannot modify header information - headers already sent by error occurs

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 hosted 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 with the first file, which prevents the second file from executing as expected.

The main reason for 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, due to the fact that programmers can (and usually does) mistakenly drive extra spaces into their code, this error occurs more often than you might expect. The # line given in the error message refers to the location of the problem - it will help you fix the problem faster and without fuss.

Can't modify header information - headers already sent by error fix

Now that you know what is causing the error, you can proceed to fix it. We will show you two options for fixing the problem, which you can try individually or one by one if it does not work separately.

Option 1 - Editing the faulty file

The first option for solving the error Warning: cannot modify header information - manual correction of the file with an error. You already have the information you need to find the problem in the error message itself (remember, this is the first file in the message). All that is required is to open this file via FTP using a client like FileZilla or via a file manager.

Basically, all you need to take care of here is to remove extra spaces / blank lines in the file. A good place to start would be the # line mentioned in the error message. From this point on, you can continue parsing the rest of the file looking for other unnecessary spaces or blank lines to the very end of the document.

Make sure the spelling of the beginning and ending is correct. pHP tags... There must be no space before or after the tag as well as the tag ?> ... Also, the last line of code must not end with a space or extra line feed.

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


PROMPT: In many text editors you can remove unnecessary spaces automatically. For example, to remove extra spaces in the Atom editor, highlight all the code and go to P ackages -\u003e Whitespace -\u003e Remove Trailing Whitespace.

Option 2 - Replace the faulty file

Of course, editing a number of misspelled files can be difficult. The files can be related to a plugin or theme you just installed on your site, or they can even be WordPress core files.

If the error is indeed caused by a plugin or theme, all you have to do is reinstall him / her. 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 error file in your installation with the one in good condition. This will ensure that the faulty file is restored to the initial state, while the rest of your WordPress site installation will remain intact. Now, just reload the page and make sure the error is fixed.

In conclusion

Whether you pasted the code snippet into a file, added a new plugin / theme, or wrote the code by hand, there is a risk of extra spaces appearing in the file. These seemingly innocent spaces could be a WordPress bug Warning: cannot modify header information - headers already sent by.

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

Author

Elena has a professional technical education in the field information technologies and programming experience in different languages \u200b\u200bfor different platforms and systems. More than 10 years devoted to the web, working with different CMS, such as: Drupal, Joomla, Magento and of course the most popular content management system today - WordPress. Her articles are always technically accurate and accurate, whether it's a WordPress overview or how to set up your VPS server.

Submitted by on Thu, 05/04/2017 - 12:55

Description of a particular problem

After clicking on the button, an error is displayed:

Warning: Cannot modify header information - headers already sent by (output started at C: \\ OpenServer \\ domains \\ testsite \\ WEB \\ 5_phpRedirect.php: 10) in C: \\ OpenServer \\ domains \\ testsite \\ WEB \\ 5_phpRedirect.php on line 12

The code is similar to that given in this topic:

Experimental Web

Select a script to download

Handler script:

When does it happen

Error (warning) like:

Warning: Cannot modify header information - headers already sent by

It occurs if you have already done something that requires the installation of browser headers, and now you want to overwrite them with new ones. For example, if you have already displayed the text, then php sets the headers (in particular, the header Location - which shows whether to stay on the requested page or whether you need to go to another page and get a response to the request already there) in order to show the client's browser (in its response) how to behave.

The root of the problem

Most likely, the problem in your case is that you are already serving content (html tags that are mixed in the file with the script) before commands:

Echo header ($ redirect);

Remember that the header () function can only be called if the client no data has been transmitted yet... That is, it should go first in the output, there should not be any HTML tags, empty lines, etc. before calling it. Quite often, an error occurs when, when reading the code file functionslike include or require, there are spaces or empty lines in this code that are printed before calling header (). The same problems can occur when using a single PHP / HTML file.

That is, it is necessary to rid the handler script of html - after all, in fact, it does not output anything itself, but simply transfers it to another address - this is the first.

Header ($ redirect);

Echo header ($ redirect);

Experiment

Since echo () actually writes to http response body, and not in headers, and header returns void (that is, does not return values), as mentioned above, then there is no point in using echo (), but
- nevertheless, I propose to conduct an experiment:

  1. remove the html
  2. do not remove echo

Since header () is called for you, in fact, before echo () (since header () is an argument to echo ()) and therefore return - at the same time we will check if the function returns null - whether it will be interpreted as an empty string or (what or rather) echo won't even start working as a redirect has already occurred.

Let's clarify the reason again

Those. before calling header () no content should be displayed (what is written about the function description: http: //php.net/manual/ru/function.header ...)

  • 1) neither with echo
  • 2) not using the usual dumping of html-text into the browser.

In our case, apparently, echo does not affect anything, but the html in the handler even does.

the issue was resolved

On your advice were deleted hTML tags... Now the redirection occurs correctly, the handler script looks like this:

The echo function doesn't really affect performance, i.e. can be left as with S. Holzner:

Also, when formatting the code for a redirect, you should pay attention to the extension of the file to which the transition is made: with the proposed syntax, it should be specified in the header argument.

  • Log in to post comments

But you can make it work and

But you can make the previous handler work as well.

User redirection

if you set the option in the php.ini file

Output_buffering \u003d 4096

  • Log in to post comments

Re-sending headers is prohibited, HTTP - the protocol doesn't work like that! But what to do then? If, after displaying on the page, you also need to start the session and set a cookie? - Remembering output buffering.

That is, when a non-zero buffer size is set, then until it is full there is still an opportunity to manipulate headers. If the buffer size is zero, after the content is displayed, its return immediately preceded by passing the http response headers to the client.

And it turns out that we want to change the headers that have already "flown away" over the network to the client (which means that it is no longer possible to fix them - in particular, the header Location, which indicates whether to stay on the requested page, or to request another - the response of the "redirector" script (for us this is a form handler) just says that we need to request another page), which is what php warns us about.

But: of course, it is impossible to solve the problem in this way (not very correctly, more precisely).

_____________
matfak vgu and the rest of the classics \u003d)

  • Log in to post comments