How to solve the problem that PHP cannot modify the header information. In actual use, for example, the following PHP error message cannot be modified: Warning: Cannotmodifyheaderinformation-headersalreadysentby we have just started to write PHP programs in actual use
For example, the following PHP error warning cannot modify the header information:
Warning: Cannot modify header information-headers already sent
We may have encountered this problem when we were writing PHP programs. Literally, it means:
Warning: the header information cannot be modified.-The headers has been sent...
So what is the cause of this warning that PHP cannot modify the header information!
That is, when we have output content before the header () or setcookie () function, for example:
- <? PHP
- Echo "hello ";
- Header ("content-type:
Text/html; charset: UTF-8 ");
- ?>
The above code generates a warning!
Why is there any output before the header and setcookie! If you understand the PHP processing process, you can easily understand it!
So how does PHP handle PHP's inability to modify header information?
When the script has any output (first output), PHP will first send the header information to the client and then send the output content (that is, the main content in the http protocol) this is because you cannot modify the sent header information. Therefore, we cannot use the header and setcookie functions to modify the header to do anything!
Let's solve the problem that PHP cannot modify the header information!
The first method is very simple! That is, try to avoid any output content before the header and setcookie. Try to write them in front.
The second solution is to use the outbuffer output buffer of PHP. the output buffer of PHP is like this. put all the output content of the current script into the outbuffer, after the program is executed, the header and outbuffer are sent to the client.
There are two ways to do this: in PHP. enable outbuffer output_buffering in ini. the default value 0 can be set to Off or On. to limit the maximum value of the output buffer, you can set this option to the specified maximum number of bytes (for example, output_buffering = 4096 ).
Another way for PHP to modify the header information is to enable it in the PHP script:
Call the ob_start () function at the beginning of the program or the beginning of a public file ();
In this way, we can enable the PHP output buffer and perform any operations below.
- <? PHP
- Ob_start ();
- Echo "dfdfd ";
- // Note that you cannot uninstall ob_start ().
- Header ("content-type: text/
Html; charset = utf-8 ");
- Setcookie ();
- ?>
If you want to start gzip, you can add the ob_gzhandler callback function ob_start ("ob_gzhandler") for ob_start ");
There are also some functions about outbuffer:
Ob_flush ()
Sending output buffer)
Ob_end_flush ()
Sends the output buffer and disables the output buffering mechanism.
Ob_end_clean ()
Clear the output buffer but not send it, and disable output buffering.
Ob_get_contents ()
Returns the current output buffer as a string. Allows you to process any output from the script.
Ob_get_clean ()
Returns the current output buffer as a string. Allows you to process any output from the script and disable the output buffering mechanism.
For more functions, refer to the PHP Manual to search for ob _
- < ?PHP
- ob_start();
- print "Here's a pretty dumb way
to calculate the length of a string.";
- $length = strlen(ob_get_content());
- ob_end_clean();
- ?>
This PHP example of failing to modify the header information shows a very inefficient method for determining the string length. Instead of simply using the strlen () function, it first starts the output buffering mechanism, prints the string, and then determines the length of the output buffer. Finally, clear the output buffer (not sent) and disable the output buffering mechanism.
Warning: Cannot modify header information-headers already sent by we are writing PHP programs just now...