Cookies are not saved. What's wrong.

Mistake

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

  • 3
    it means before that it was derived. Check classic problems - before the first <?php there are no spaces or empty lines. - after the last ?> no spaces or empty lines. - Your code does not display with echo / pring something before installing the cookie - that the text is saved without the BOM. If you use a notebook or the like, they can add to the beginning if you save it in UTF-8. - KoVadim
  • Classic problems - before the first <?php there are no spaces or empty lines. After the last ?> No spaces or empty lines. - Solved the problem, were there empty lines after ?> . - user218407

3 answers 3

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

PHP, in turn, in the course of the work should form both the headers and the body of the response. Even if you do not specify a single header, PHP itself installs all the necessary headers to respond to the client. At the same time, he writes the data to the client first to the buffer , but if he started writing the response body to the buffer, he can’t write the headers there and encountering an attempt to write response headers after the response body has been formed PHP returns 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 was the 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 in front of <? spaces or line breaks, which will also be perceived as the response body.

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

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

    Index.php file

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

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

      include( "include.inc" ); // include.inc может формировать тело ответа header( ... ); 
  6. If error output is configured in the browser, then the warning will also be the body of the response:

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

What to do if the header needs to be set exactly after you started to form the body of the answer:

  1. Create your buffer variable and place the answer in it:

     $response= "<html"; header( ... ); echo $response; 
  2. Use output buffering explicitly:

     <? ob_start();?> <html> <?php echo "<body>"; header("Content-Type: text/html; charset=utf-8"); ob_end_flush(); 
  3. You can force 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, having exceeded the buffer size, you will see the error described above.

     output_buffering = 14096 
  • but you can add something in php.ini output_buffering = 14096 and do nothing :) but in general, yes, there shouldn't be such situations - Alex Kapustin
  • and in one day you will record 14097 bytes and you will be puzzling for a long time. > but in general, yes, there should be no such situations whatsoever is a birth trauma php ... - KoVadim
  • >> and one day write 14097 bytes and you will be puzzled for a long time. in recent versions of PCP - it is installed by default - Alex Kapustin
  • We discussed in the chat ( once and twice ) whether it is not worth hanging the php-faq tag on this question and using your answer as a reference closet for such questions by mistake cannot modify header info. Will you take your link to cannot-modify-header-information.ru and add (with it or another site) the missing information? - AK
  • Add, if you consider it necessary ... I just didn’t take the information from there, but from my head, well, my answer considers more typical errors than the indicated site. - ReinRaus

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

  • Detailed answer ... - ReinRaus
  • minus me if I'm wrong) - Kirill Korushkin
  • Technically you are undoubtedly right)))) - ReinRaus
  • I'm new here, I'll learn - Kirill Korushkin
  • 3
    Holmes: - This is a programmer. - Damn it Holmes, you guessed it? - Elementary Watson. He gave an absolutely accurate, but completely useless answer. - AK

In php.ini output_buffering = On And everything works