Home php Cannot modify header information - headers already sent by

Cannot modify header information – headers already sent by

Author

Date

Category

Cookies are not saved. What’s wrong.

Error

Warning: Cannot modify header information – headers already sent by (output started at blocksSite / main_noauth.php: 4) in blocksSite / main_noauth.php on line 58


Answer 1, authority 100%

Communication between the WEB-server and the client in this case occurs via the HTTP protocol . HTTP includes HTTP headers and response body. In this case, the headers must necessarily follow the body of the response – this is required by the standard.

PHP, in turn, in the process of work must form both the headers and the body of the response. Even if you do not specify any headers, PHP sets all the necessary headers to respond to the client by itself. At the same time, the data that must be given to the client, he writes first in buffer , but if he began to write the response body to the buffer, then he cannot write the headers there, and upon encountering an attempt to write the response headers after the PHP response body has begun to form, it gives an error:

Warning: Cannot modify header information - headers already sent by (output started at ...

This may be due to the following reasons:

  1. Before setting the headers, the response body was output. In this case, the output can be carried out both by means of the template engine, and through the functions echo or print

    & lt; html & gt; // output by means of the template engine
    & lt;? php
      echo "& lt; body & gt;"; // output by means of the operator
      header ("Content-Type: text / html; charset = utf-8");
    
  2. You can simply make a mistake and put spaces or line breaks before & lt ;? , which will also be perceived as the body of the response.

  3. Exactly the same as in point 2, but at the end of the php file:
    incrementX.php

    file

    & lt;? php
    $ x ++;
    ? & gt; [space] [line break]
    

    File index.php

    & lt;? php
    include ("incrementX.php");
    header (...);
    
  4. Files that contain PHP code must be saved without BOM . How to save a file without BOM .

  5. Do not forget that if you include files via include or require , the files accessed by these functions may also contain one of the above three problems.

    include ("include.inc"); // include.inc can form the response body
     header (...);
    
  6. If errors are configured to be output to the browser, then warning will also be the body of the response:

    $ filename = "";
     $ text = file_get_contents ($ filename); // Warning: file_get_contents (): Filename cannot be empty
     header (...);
    

The most correct solution to most problems with the output of headers is to change the structure of the PHP file so that all output begins only after the main script logic has worked.

If you have to work with legacy code where everything is mixed up, you can explicitly use output buffering:

& lt ;? ob_start ();? & gt;
  & lt; html & gt;
  & lt;? php
    echo "& lt; body & gt;";
    header ("Content-Type: text / html; charset = utf-8");
    ob_end_flush ();

or by setting php.ini to force PHP to first fill the buffer with data up to a certain size, and only then send the contents of the buffer to the client, but in this case, exceeding the buffer size, you will see the above described error.

output_buffering = 14096

Answer 2, authority 12%

you have something echoed before sending the header ... main_noauth.php on line 58 ... . Look at line 58


Answer 3

In php.ini

output_buffering = On

And everything works

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions