PHP error: Warning: cannot modify header information-headers already sent...

Source: Internet
Author: User

If you see this warning when executing the PHP program: "Warning: cannot modify header information-headers already sent ...."

Few notes based on the following user posts:
There are several solutions:

1. blank lines (blank line ):
Make sure no blank line after <? PHP...?> Of the calling php scr limit PT.
Check whether <? PHP...?> There are no blank lines, especially include or require files. Many problems are caused by these blank rows.

2. Use Exit Statement (Use Exit to solve ):
Use Exit after header statement seems to help some people
Add exit () after the header ();
Header ("Location: XXX ");
Exit ();

3. PHP has this annoying problem, if your HTML goes before any PHP code or any header modification before redirecting to certain page, it ll said "Warning: cannot modify header information-headers already sent .... "Basically anytime you output to browser, the header is set and cannot be modified. so two ways to get around the problem:

3A. Use Limit Cr limit Pt (solve with limit Cr limit pt ):
<? Echo "<SCR platinum Pt> self. Location (file. php); </scr platinum Pt>";?>
Since it s a SCR limit PT, it won t modify the header until execution of your Cr limit PT.
You can replace the header with javascr into PT. In addition, it should be noted that the browser needs to support javascr into pT.

3B. Use output buffering (solved by the output cache ):
<? PHP ob_start ();?>
... Html codes...
<? PHP
... PHP codes...
Header ("Location :....");
Ob_end_flush ();
?>
This will save the output buffer on server and not output to browser yet, which means you can modify the header all you want until the ob_end_flush () statement. this method is cleaner than the specified CR has PT since has CR has PT method assumes the browser has CR has PT turn on. however, there are overhead to store output buffer on server before output, but with modern hardware I wowould imagine it won t be that big of deal. when Cr else PT solution wocould be better if you know for sure your user has your Cr else PT turn on their browser.

As in the code above, this method is cached when the page is generated, so that the header can be output after the head is output. This method is used to solve the header problem.

4. Set output_buffering = on in PHP. ini (enable output_buffering in PHP. INI)
Set output_buffering = on will enable output buffering for all files. but this method may slow down your PHP output. the performance of this method depends on which web server you re working with, and what kind of SCR limit PTS you re using.
This method is the same as 3B in theory. However, This method enables the output cache of all PHP programs, which may affect the PHP Execution efficiency, depending on the server performance and Code complexity.

 

 

Second:

How to completely eliminate warning: cannot add header information-headers already sent in ...... This is a confusing error. As long as you have written PHP code, I believe you have met this extremely confusing warning. Today, let's get it done ............... After reading the PHP manual, the answer is as follows: "Warning: cannot send session cookie-headers already sent ..." Or "cannot add/modify header information-headers already sent ...". The function header (), setcookie (), and session functions must add header information to the output stream. However, the header information can only be sent before any other output content. Before using these functions, there cannot be any (such as HTML) output. The headers_sent () function can check whether your script has sent the header information. See "output control functions ". Do not use any text, blank lines, carriage return, or spaces before using the above function. But... The question is, this answer is not satisfactory. Because programs often run normally in other PHP environments. First, how is this error generated? Let's take a look at how PHP handles HTTP header output and subject input.
This article discusses how to completely eliminate warning: cannot add header information-headers already sent in ...... This is a confusing error.
As long as you have written PHP code, I believe you have met this extremely confusing warning. Today, let's get it done ...............
After reading the PHP manual, the answer is as follows:
Message "Warning: cannot send session cookie-headers already sent ..." Or "cannot add/modify header information-headers already sent ...".

The function header (), setcookie (), and session functions must add header information to the output stream. However, the header information can only be sent before any other output content. Before using these functions, there cannot be any (such as HTML) output. The headers_sent () function can check whether your script has sent the header information. See "output control functions ".

Do not use any text, blank lines, carriage return, or spaces before using the above function. But... The question is, this answer is not satisfactory. Because programs often run normally in other PHP environments.

First, how is this error generated? Let's take a look at how PHP handles HTTP header output and body output.

When the PHP script starts to be executed, it can send both header and subject information. Header information (From header () or setcookie () function) is not sent immediately. Instead, it is saved to a list. This allows you to modify the title information, including the default title (for example, Content-Type title ). However, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate the HTTP header. Then send the subject data. At this time, any attempt to add or modify the header information is not allowed, and one of the above error messages will be sent.

Good! Let's solve it: stupid method: Do not display all error warnings! Measure
Error_reporting (e_error | e_parse); Do not display e_warning here.

 

Solution:
1) applicable to PhP editing with permissions. INI user
Open PHP. INI file (You Should Know Your PHP better than me. INI), find
Output_buffering = changed to on or any number. If it is IIS6, change it to on. Otherwise, your PHP efficiency will be very slow.

 

2) use a VM and cannot edit PHP. INI, what should I do?

Simple:
Create one under the root directory of your space. The content of the htaccess file is as follows:

AllowOverride all
Php_flag output_buffering on

Unfortunately: or not? Cannot all webpages be displayed?

Then, use the following method:

Add ini_set ("output_buffering", "1") at the beginning of the PHP file ");

Open the PHP output cache for this page.

3) solve the problem in the PHP File

Ob_start ()
Enable the output buffering mechanism. Output buffering supports multiple levels-for example, you can call the ob_start () function multiple times.

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.

Principle:
When output_buffering is enabled, PHP does not send HTTP headers when the script sends the output. Instead, it inputs this output through the pipeline (PIPE) to the dynamically added cache (only in PHP 4. 0, which has a central output mechanism ). You can still modify/Add the header or set the cookie because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the content in the output buffer.

 

4) Kill Techniques
If none of the above methods can be used for satisfactory solutions, use the following methods:

Open the problematic webpage in notepad and save it as an ANSI encoded file of the same name.
Save the file as a UTF-8-encoded file with editplus.
Try again. It should be displayed.
There are two main causes:
1. Other content is output between the header () function (usually hidden by the browser), so that subsequent headers cannot send new page types again. This can be solved by enabling output_buffering. method 2) and 3) are the same.
2: PHP files using UTF-8 encoding, because the encoding is not compatible (especially through other encoding conversion ), generate the BOM "there is a character called" Zero Width no-break space "in the UCS encoding, and its encoding is feff. Fffe does not exist in the UCs, so it should not appear in actual transmission. We recommend that you transmit the character "Zero Width no-break space" before transmitting the byte stream in the UCS specification ". In this way, if the receiver receives feff, it indicates that the byte stream is big-Endian; if it receives fffe, it indicates that the byte stream is little-Endian. Therefore, the character "Zero Width no-break space" is also called Bom.
The UTF-8 does not need BOM to indicate the byte order, but BOM can be used to indicate the encoding method. The UTF-8 code for the character "Zero Width no-break space" is ef bb bf. So if the receiver receives a byte stream starting with ef bb bf, it will know that this is UTF-8 encoding.

Windows uses BOM to mark the encoding method of text files. The header file is not correctly identified, as long as the BOM in the UTF-8 file can be removed, Method 4) is based on this principle.

Output. When the PHP script starts to be executed, it can send both header and subject information. Header information (From header () or setcookie () function) is not sent immediately. Instead, it is saved to a list. This allows you to modify the title information, including the default title (for example, Content-Type title ). However, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate the HTTP header. Then send the subject data. At this time, any attempt to add or modify the header information is not allowed, and one of the above error messages will be sent. Good! Let's solve it: stupid method: Do not display all error warnings! Error_reporting (e_error | e_parse); Do not display e_warning here. Solution: 1) applicable to PhP editing with permission. INI people open PHP. INI file (You Should Know Your PHP better than me. INI), find output_buffering = To on or any number. If it is IIS6, change it to on. Otherwise, your PHP efficiency will be very slow. 2) use a VM and cannot edit PHP. INI, what should I do? Simple: Create one under the root directory of your space. The content of the htaccess file is as follows: AllowOverride all php_flag output_buffering on is unfortunately: or not? Cannot all webpages be displayed? Then, use the following method: Add ini_set ("output_buffering", "1") at the beginning of the PHP file to open the PHP output cache. 3) Enable the output buffering mechanism for ob_start () in the PHP file. Output buffering supports multiple levels-for example, you can call the ob_start () function multiple times. Ob_end_flush () sends the output buffer and disables the output buffering mechanism. Ob_end_clean () clears the output buffer but does not send it, and disables output buffering. Ob_get_contents () returns the current output buffer as a string. Allows you to process any output from the script. Principle: When output_buffering is enabled, PHP does not send HTTP headers when the script sends the output. Instead, it inputs this output through the pipeline (PIPE) to the dynamically added cache (only in PHP 4. 0, which has a central output mechanism ). You can still modify/Add the header or set the cookie because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the content in the output buffer. 4) If none of the above methods can be used to solve the problem, use the following method: use NotePad to open the problematic webpage and save it as an ANSI encoded file of the same name. Save the file as a UTF-8-encoded file with editplus. Try again. It should be displayed. The cause is mainly caused by the following two points: 1. Other content is output between header () functions (usually hidden by the browser ), as a result, subsequent headers cannot send new page types again. This can be solved by enabling output_buffering. method 2) and 3) are the same. 2: PHP files using UTF-8 encoding, because the encoding is not compatible (especially through other encoding conversion ), generate the BOM "there is a character called" Zero Width no-break space "in the UCS encoding, and its encoding is feff. Fffe does not exist in the UCs, so it should not appear in actual transmission. We recommend that you transmit the character "Zero Width no-break space" before transmitting the byte stream in the UCS specification ". In this way, if the receiver receives feff, it indicates that the byte stream is big-Endian; if it receives fffe, it indicates that the byte stream is little-Endian. Therefore, the character "Zero Width no-break space" is also called Bom. The UTF-8 does not need BOM to indicate the byte order, but BOM can be used to indicate the encoding method. The UTF-8 code for the character "Zero Width no-break space" is ef bb bf. So if the receiver receives a byte stream starting with ef bb bf, it will know that this is UTF-8 encoding. Windows uses BOM to mark the encoding method of text files. The header file is not correctly identified, as long as the BOM in the UTF-8 file can be removed, Method 4) is based on this principle.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.