php – Cannot modify header information – headers already sent by


Cookies are not saved. What's wrong.


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


In this case, communication between the WEB-server and the client takes place over the HTTP protocol . HTTP includes HTTP headers and response bodies. In this case, the headers must necessarily follow the body of the response – this is required by the standard.

PHP, in turn, must form both the headers and the body of the response in the process. Even if you do not specify any headers, PHP itself sets all the necessary headers to respond to the client. In this case, he first writes the data that must be given to the client into the buffer , but if he began to write the response body to the buffer, then he cannot write the headers there in any way, and when he meets an attempt to write the response headers after the response body has begun to form, PHP issues 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, there was an output of the response body. In this case, the output can be carried out both by means of the template engine, and through the echo or print functions

     <html> // вывод средствами шаблонизатора <?php echo "<body>"; // вывод средствами оператора header( "Content-Type: text/html; charset=utf-8" );
  2. You can simply make a mistake and put before <? spaces or line breaks, which will also be interpreted 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

     <?php $x++; ?>[пробел][перенос строки]

    Index.php file

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

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

     include( "" ); // может формировать тело ответа header( ... );
  6. If you are configured to output errors 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 in such a way that all the output starts only after the main logic of the script has worked.

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

    <? ob_start();?>
        echo "<body>";
        header("Content-Type: text/html; charset=utf-8");

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 
Scroll to Top