I often encounter this problem when writing PHP programs;
Including the use of the header() function, the session() function, and the cookies function, there may be problems
The PHP program reports the following error:
Cannot modify header information - headers already sent by (output started at
Cannot modify header infomation. To solve this error, you need to understand two issues.
First, http protocol
In the http protocol , the server outputs an html file to the browser. The html file consists of two parts. One part is the data of our page itself, and the other part is the header information of the html page. This information contains a lot of data, such as what encoding format the page is displayed in, HTML data size, whether to jump, whether to cache, etc. These html header file information are not allowed to be modified once generated. This is the reason for the above error, Cannot modify header information,
Second, PHP’s caching mechanism
When the PHP program is running, it has two parts of cache. One is the program cache, which I understand as the data cache of the program in the memory; the other is the cache module provided by PHP. Through this module, PHP will open up another place as Data caching. If the PHP module cache is turned on, when there is echo data in the program, the data will be put into the cache instead of directly generating HTML files. Until the PHP program is finished running, the module cached data will be generated together with HTML. Code, including header files. Here are two examples:
1. The cache is not turned on (the default is this)
<?php echo "123"; header("Content-type:text/html;charset=utf-8") echo "456"; ?>
The Cannot modify header information error is exposed at this time;
2. Turn on the caching mechanism
<?php ob_start() ; //开启页面缓存 echo "123"; header("Content-type:text/html;charset=utf-8") echo "456"; ?>
In the above two examples, the cache state is not enabled, and an HTML code has been generated when echo "123"; is reached. At the third line, the header function cannot modify the header file information and an error is reported; in the example In 2, ob_start(); turns on the cache, (ob is output_buffer), when echo "123", the data is written to the cache module, and then the header() function runs. At this time, the HTML page is not generated, and then echo "456"; It is also output to the cache module. After the program is executed, the data of the cache module will generate a complete HTML page, so that no error will be reported.
Second, PHP’s caching mechanism function and how to enable it
as mentioned above. As mentioned above, the ob_start() function can open the cache module, but this function only opens this page. If necessary, you can modify the php.ini file, find the output_buffering option, and modify it to output_buffering = 4096 (On is also OK). The number represents the cache size. .
The PHP caching mechanism also has some functions. You can try it to better understand the PHP caching mechanism.
ob_start() starts output buffering. At this time, PHP stops outputting, and all subsequent outputs are transferred to a In the internal buffer.
ob_get_contents() This function returns the contents of the internal buffer. This is equivalent to turning these outputs into strings.
ob_get_length() returns the length of the internal buffer.
ob_end_flush() ends the output Buffer, and output the contents in the buffer. After this, the output is normal output.
ob_end_clean() ends the output buffer and throws away the contents in the buffer.