Ugly information headers. What are Http headers?

Ugly information headers. What are Http headers?

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:

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

Description of a particular problem

After clicking the button, an error appears:

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 script to download

Handler script:

When does this happen

Error (warning) type:

Warning: Cannot modify header information - headers already sent by

Occurs if you have already done something that requires setting browser headers, and now you want to rewrite them with new ones. For example, if you have already displayed 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 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 transferred yet. That is, it should come first in the output; there should be no HTML tags, empty lines, etc. before its call. Quite often an error occurs when, when reading the code file functions, like include or require, this code contains spaces or empty lines that are printed before header() is called. The same problems can arise when using a single PHP/HTML file.

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

Header($redirect);

Echo header($redirect);

Experiment

Since echo() generally writes to http response body, and not in the 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 html
  2. don't remove echo

Since header() is essentially called before echo() (since header() is an argument to echo()), which means return - at the same time, let's check if the function returns null - will it be interpreted as empty line or (which is more correct) echo will not even start working until the redirect has already occurred.

Let's clarify the reason again

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

  • 1) neither with echo
  • 2) not by simply dumping html text into the browser.

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

the issue has been resolved

On your advice they were removed HTML tags. Now the redirection occurs correctly, the handler script looks like this:

The echo function doesn't really affect the operation, i.e. You can also leave it as in S. Holzner:

Also, when writing 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 must be specified in the header argument.

  • Log in to post comments

But you can make it work

But you can also make the previous handler work

User redirection

if you set the option in the php.ini file

Output_buffering = 4096

  • Log in to post comments

Resending headers is prohibited, the HTTP protocol doesn't work that way! But what should we do? If, after displaying on the page, you also need to start a session and set a cookie? - Let's remember about output buffering.

That is, when a non-zero buffer size is set, then until it is full it is still possible to manipulate the headers. If the buffer size is zero, after the content is output, it is returned immediately preceded by passing HTTP response headers to the client.

And it turns out that we want to change the headers that have already “flown” over the network to the client (which means it is no longer possible to correct them - in particular the header Location, which indicates whether to stay on the requested page or request another one - 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).

_____________
Mathematics Faculty of VSU and other classics =)

  • Log in to post comments

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 with there will be 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 PHP start and end tags correctly. There should be no space before or after the tag , as well as tag ?> . Also, last line The code should not end with a space or an extra line break.

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


CLUE: In many text editors You can remove unnecessary spaces automatically. For example, to delete 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 a professional technical education in area 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 various CMSs, 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.

This error message is often encountered by programmers starting to use PHP. Understanding why this error occurs will help you find a solution.

PHP does a lot of the work of generating web pages for you, without you even asking. A web page consists of two parts: a header and a body.

This common PHP error is observed when the programmer makes mistakes in the manipulation or creation of headers. Here's an example:

Warning: Cannot modify header information – headers already sent by (output started at /home/usr1/public_html/sent.php:42) in /home/usr1/public_html/includes/theme-header.php on line 12

Typically, you don't need to worry about the header, since it is automatically generated and contains information about the page, server, and cookie. The information in the header is important, but it is usually not visible to the user. Here are some examples:

Date: Mon, 10 Jul 2006 18:51:59 GMT Server: Apache/2.2.0 (Unix) mod_ssl/2.2.0 OpenSSL/0.9.7g Content-Encoding: gzip Content-Type: text/html

Sometimes programmers want to change some header values. For example, if PHP generates XML output, the Content-Type must be changed to indicate this. Another common example is to redirect the user's browser to another web page using the Location header element, as described in this article.

The header must come first in the response from the web server and is separated from the body by one blank line. The reason for this error is that some part of the web page body has already been sent to the user before an attempt is made to set the header value. Since PHP simplifies many things for you, the problem may be lurking in the usual place. Here are some guidelines for finding the problem:

  1. Find the header() statement that is causing the problem. The error must be on or before this line.
  2. Look for any instructions that might route output to the user before this header statement. If you find one or more, change the code to move the header instruction before them. Complex conditional statements can complicate the problem, but they can also help solve the problem. Alternatively, you can use a conditional expression at the top of the PHP script that determines the value of the header as early as possible, and sets it there.
  3. Make sure there are no spaces outside the PHP start and end tags. While the empty line before the start tag
  4. If you save your file in UTF-8 encoding, then make sure that the file is saved without a signature (without BOM). A signature is a byte added at the beginning of the file, and if the PHP script is saved in this format, then this byte will be perceived as part of the output of the page body, which should not be allowed to avoid the problem we are considering.

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 you translate this error into Russian, you will 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. This function is also 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"and 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?