PHP Error Warning: Cannot modify header information headers already sent by Solution

  • 2021-07-21 07:38:51
  • OfStack

I encountered this error while testing the following code today:


session_start();
$_SESSION['username']=$username;
echo "<script language='javascript'>location.href='../admin.php';</script>";
exit();

An error occurred:


Warning: Cannot modify header information - headers already sent by...

After reading some online methods, it didn't solve it. Finally, when output_buffering is configured to 4096 by default in php. ini, this error was not encountered:

output_buffering setup instructions:

Off: Indicates closing PHP output cache
On: Open an infinite output cache
4096: Open output cache with size 4096Byte

By default, php buffer is turned on, and the default value of buffer is 4096, that is, 4kb. You can find the output_buffering configuration in the php. ini configuration file. When echo, print and so on output user data, the output data will be written to php output_buffering until output_buffering is full, and these data will be transmitted to the browser for display through tcp. You can also manually activate the php output_buffering mechanism through ob_start (), so that even if the output exceeds 4kb data, it does not really give the data to tcp for transmission to the browser, because ob_start () sets the php buffer space large enough. The data is not sent to the client browser until the script ends or the ob_end_flush function is called.

For more information about output_buffering, please refer to:

https://www.ofstack.com/article/55707.htm

Add: Of course, you can also solve this problem by removing BOM. It is recommended to encode in UTF-8 without BOM format. Thank you @ ihipop children's shoes
With regard to BOM, simply speaking, the software uses BOM to identify whether this file is UTF-8 encoded. In earlier versions of Firefox, extensions did not have BOM, but BOM has been supported since Firefox 1.5. Now it is found that PHP does not support BOM either. PHP was designed without BOM in mind, meaning that it does not ignore the three characters of BOM at the beginning of the UTF-8 encoded file.

There is another trouble mentioned: "Due to the limitation of COOKIE sending mechanism, among the files with BOM at the beginning of these files, COOKIE cannot be sent out (because PHP has sent out the file header before COOKIE is sent out), so the login and logout functions are invalid. 1 relying on COOKIE, all the functions realized by SESSION are invalid." This should be the reason for the blank page in the background of Wordpress, because any one executed file contains BOM, these three characters will be sent out, resulting in the failure of the function relying on cookies and session, so you may also encounter the following errors:


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at E:\web\index.php:1) in E:\web\functions\sessions.php on line 39


Related articles: