PHP error Warning: Cannot modify header information headers solution
[code].quot; Warning: Cannot modify header information - headers already sent by...
open php.ini file, add
[code]output_buffering =on[/code]
save, upload and overwrite.
http://blog.csdn.net/besily/archive/2010/03/19/5396268.aspx
Few notes based on the following user posts:
there are the following solutions. P SCR / pt.
check.Lt; PHP....gt; there are no blank lines, especially include or require files. A lot of the problems are caused by these blank lines.
2. Use exit statement (solved by exit):
Use exit after header. Modification before redirecting to certain page, it ll said.Quot, and Warning: Cannot.
.lt;
.lt; echo.Quot;.Lt; SCR pt.gt; self.location (file.php);.Lt; Also, this approach requires the browser to support Javascr pt.
3b. Use output buffering (solved by the output cache):
.lt;? PHP ob_start ();?.gt; .gt,
This will save the output buffer on server and not. Are overhead to store output buffer on server before output. The cache is generated when the page is generated, which allows output of header after the output of head. The wishing board is the header problem solved by this method.
4.set output_buffering = On in php.ini (output_buffering in php.ini)
set output_buffering. T kind of SCR PTS you re using.
this method is the same as that of 3B. But this approach opens up the output caching of all PHP programs, which may affect the efficiency of PHP execution, depending on the performance of the server and the complexity of the code.
second kinds:
how to completely eliminate warning: Cannot add header information headers already already information... Such an inexplicable mistake. As long as you have written PHP code, I believe you have met this warning which is often very inexplicable. Today we will fix it. Read the PHP manual and answer the following message: "Warning:Cannot send session cookie headers already sent..." Or "Cannot add/modify header information - headers already sent...". The function header (), setcookie () and session function need to add header information in the output stream. But header information can only be sent before any other output content. You can't have any output (such as HTML) before using these functions. Function headers_sent () can check whether your script has sent header information. Please refer to the "output control function". Meaning: do not use any text, empty line, carriage return, space, etc. before using the function above. But. The question is, the answer is not satisfactory. Because programs often run in other PHP environments. First of all: how does this mistake come from? Let's take a look at how PHP handles HTTP header output and the body loses
how to completely eliminate warning: Cannot add header information headers already sent. Such an inexplicable mistake.
as long as you have written the PHP code, I believe you have met this warning that is very inexplicable most of the time. Today we are going to fix it...
read the PHP manual, answer as follows:
message "Warning:Cannot send session Cookie - headers already sent..." Or "Cannot add/modify header information - headers already sent...".
function header (), setcookie () and session function need to add header information in the output stream. But header information can only be sent before any other output content. You can't have any output (such as HTML) before using these functions. Function headers_sent () can check whether your script has sent header information. Please refer to the "output control function".
means: do not use any text, empty line, enter, space, etc. before using the function above. But. The question is, the answer is not satisfactory. Because programs often run in other PHP environments.
First: How did this mistake come about? Let's see how PHP handles HTTP header output and main output. When
PHP script is executed, it can send header (header) information and main body information simultaneously. Header information (from header () or SetCookie ()) is not sent immediately, instead, it is saved to a list. This allows you to modify the header information, including the default title (for example, the Content-Type title). However, once the script sends any non - Title Output (for example, using HTML or print () calls), then PHP has to send all the Header first and then terminate HTTP header. Then continue to send the body data. From this point on, any attempt to add or modify Header information is not allowed, and one of the above error messages will be sent.
OK! Let's solve it: the stupid method: the error warning doesn't show up!
error_reporting (E_ERROR E_PARSE); do not display E_WARNING,
solution:
1) applicable to privileged editing PHP. INI people
Open PHP. INI file (you should try to know your PHP more than I do. " Where is ini?) find
output_buffering = change to on or any number. If it is IIS6, please change it to ON, otherwise your PHP efficiency will be extremely slow.
2) cannot use virtual host to edit PHP. INI, what do you do?
simple:
create one in your space root directory. The htaccess file is as follows:
AllowOverride All
PHP_FLAG output_buffering On
unfortunately, is it still not? All the web pages can't be displayed?
then, then use the following method:
at the beginning of the PHP file: ini_set (.Quot; output_buffering.quot;.Quot; 1.quot;);
let this page open the output cache.
3) to solve
ob_start ()
output buffering mechanism in PHP file. Output buffering supports multiple levels - for example, the ob_start () function can be called multiple times.
ob_end_flush ()
send output buffer (output buffer) and disable output buffering mechanism.
ob_end_clean ()
clear output buffer but do not send, and disable output buffering.
ob_get_contents ()
returns the current output buffer to a string. Allow you to handle any output from the script.
principle: when
output_buffering is enabled, PHP does not send HTTP header when the script sends output. Instead, it outputs this output through the pipeline (pipe) into a dynamically increasing cache (only at PHP 4). 0, it has a centralised output mechanism. You can still modify / add header, or set cookie, because header is not actually sent. When all scripts are terminated, PHP automatically sends HTTP header to the browser, and then sends the contents of the output buffer.
4) kill the trick
if all the above methods can't wait for a satisfactory solution, please use the following method:
first, open a web page with a notepad, and save as a ANSI encoded homonym file.
save the file as a UTF-8 encoded file by EditPlus.
try again, it should be shown. The cause of
is mainly caused by the following two points:
1: the other content is output between the Header () functions (usually hidden by the browser), causing the subsequent Header to not send a new page type again. This can be solved by opening Output_Buffering, method 2) and 3.
Two: the PHP file uses UTF-8 encoding, resulting in a BOM character called.Quot; ZERO WIDTH NO-BREAK SPACE.quot; the encoding of the UCS code is FEFF because of the incompatibility of the encoding (especially through other coding). FFFE is not a character in UCS, so it should not appear in actual transmission. The UCS specification recommends that we transmit the character.Quot; ZERO WIDTH NO-BREAK SPACE.quot before transferring the byte stream. In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if FFFE is received, it indicates that the byte stream is Little- Endian. So the character.Quot; ZERO WIDTH NO-BREAK SPACE.quot; also known as BOM.
UTF-8 does not require BOM to indicate byte order, but BOM can be used to indicate encoding. The character.Quot; ZERO WIDTH NO-BREAK SPACE.quot; the UTF-8 encoding is EF BB BF. So if the receiver receives the byte stream beginning with EF BB BF, it is known that this is UTF-8 encoding.
Windows is the use of BOM to mark the encoding of text files. The header file can not be correctly identified, so just remove the BOM in the UTF-8 file, and method 4 is based on this principle.
. When the PHP script starts executing, it can send header (header) information and main body information simultaneously. Header information (from header () or SetCookie ()) is not sent immediately, instead, it is saved to a list. This allows you to modify the header information, including the default title (for example, the Content-Type title). However, once the script sends any non - Title Output (for example, using HTML or print () calls), then PHP has to send all the Header first and then terminate HTTP header. Then continue to send the body data. From this point on, any attempt to add or modify Header information is not allowed, and one of the above error messages will be sent. OK! Let's solve it: the stupid method: the error warning doesn't show up! Error_reporting (E_ERROR E_PARSE); here do not display the E_WARNING solution: 1) applicable to the privileged editor of PHP. INI's people open PHP. INI file (you should try to know your PHP more than I do. " Where is ini?) find output_buffering = on or any number. If it is IIS6, please change it to ON, otherwise your PHP efficiency will be extremely slow. 2) using a virtual host, you can't edit PHP. INI, what do you do? Simple: create one in your space root directory. The htaccess file is as follows: AllowOverride All PHP_FLAG output_buffering On: Unfortunately, is it still not? All the web pages can't be displayed? Then, use the following method: at the beginning of the PHP file: ini_set (.Quot; output_buffering.quot;.Quot; 1.quot;); let this page open the output cache of the PHP. 3) solve the ob_start () output buffering mechanism in the PHP file. Output buffering supports multiple levels - for example, the ob_start () function can be called multiple times. Ob_end_flush () sends output buffer (output buffer) and disables the output buffering mechanism. Ob_end_clean () clears output buffer but does not send it and disables output buffering. Ob_get_contents () returns the current output buffer to a string. Allow you to handle any output from the script. Principle: when output_buffering is enabled, PHP does not send HTTP header when the script sends output. Instead, it outputs this output through the pipeline (pipe) into a dynamically increasing cache (only at PHP 4). 0, it has a centralised output mechanism. You can still modify / add header, or set cookie, because header is not actually sent. When all scripts are terminated, PHP automatically sends HTTP header to the browser, and then sends the contents of the output buffer. 4) kill skills if the above methods can't wait for a satisfactory solution, please use the following methods: first use Notepad to open the problem of the web page, and save as the ANSI code of the same name file. Use EditPlus to save the file as UTF-8 encoded file. Try again, it should be shown. The main reasons are the following two points: first, the other content is output between the Header () functions (usually hidden by the browser), causing the later Header to not send a new page type again. This can be solved by opening Output_Buffering, method 2) and 3. Two: the PHP file uses UTF-8 encoding, and because of the incompatibility of the encoding (especially through other coding), the BOM has produced a character called.Quot; ZERO WIDTH NO-BREAK SPACE.quot in the UCS code, and its encoding is FEFF. FFFE is not a character in UCS, so it should not appear in actual transmission. The UCS specification recommends that we transmit the character.Quot; ZERO WIDTH NO-BREAK SPACE.quot before transferring the byte stream. In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if FFFE is received, it indicates that the byte stream is Little- Endian. So the character.Quot; ZERO WIDTH NO-BREAK SPACE.quot; also known as BOM. UTF-8 does not require BOM to indicate byte order, but BOM can be used to indicate encoding. The character.Quot; ZERO WIDTH NO-BREAK SPACE.quot; the UTF-8 encoding is EF BB BF. So if the receiver receives the byte stream beginning with EF BB BF, it is known that this is UTF-8 encoding. Windows is the way to mark text files by using BOM. The header file can not be correctly identified, so just remove the BOM in the UTF-8 file, and method 4 is based on this principle.