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
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:
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" );
You can simply make a mistake and put in front of <?
spaces or line breaks, which will also be perceived as the response body.
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( ... );
Files that contain PHP code should be saved without a BOM . How to save a file without BOM .
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( ... );
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:
Create your buffer variable and place the answer in it:
$response= "<html"; header( ... ); echo $response;
Use output buffering explicitly:
<? ob_start();?> <html> <?php echo "<body>"; header("Content-Type: text/html; charset=utf-8"); ob_end_flush();
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
you have something to do before sending the header ...main_noauth.php on line 58...
Look at line 58
In php.ini output_buffering = On And everything works
Source: https://ru.stackoverflow.com/questions/284578/
All Articles
<?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<?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