Solve cannot modify header information-headers already sent (......).

Source: Internet
Author: User
Tags browser cache

The error message is often prompted: cannot modify header information-headers already sent (......). In fact, the expected results have been achieved. This error message is uncomfortable. Many methods have been found on the Internet. The comprehensive solution is as follows:
1. Add ob_start () to the PHP tag at the top of the page ();
2. Add ob_end_flush () to the returned information ();
In this way, the error information can be blocked.
In addition, the method of another person may be effective in other cases.
If you got this message: "Warning: cannot modify header information-headers already sent ...."
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 of the calling PHP script.
Check that 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 JavaScript (solved using JavaScript ):
Since it's a script, it won't modify the header until execution of JavaScript.
You can use JavaScript to replace the header. However, the above Code is not successfully executed... You need to note that using this method requires the browser to support JavaScript.
3B. Use output buffering (solved by the output cache ):

... Html codes...
... 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 Javascript since JavaScript method assumes the browser has JavaScript turn on. however, there are overhead to store output buffer on server before output, but with modern hardware I wocould imagine it won't be that big of deal. javascript solution wocould be better if you know for sure your user has JavaScript 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.
In the background management or sometimes in the Forum, click a page, the page will appear at the top
Warning: cannot modify header information-headers already sent ....
This is because of the setcookie statement.
There are some restrictions on the use of cookies, such:
1. Descriptions of the call to setcookie must be placed before the tag
2. You cannot use echo before calling setcookie.
3. The cookie will not appear in the program until the website is reinjected.
4. The setcookie function must be sent before any information is sent to the receiver.
5 .......
Based on the above restrictions, when setting the setcookie () function, we often encounter "undefined Index", "cannot modify header information-headers already sent "... To solve this problem, the method of "cannot modify header information-headers already sent by" is before the cookie is generated, extend the token to the receiver. Therefore, you can add ob_start (); at the beginning of the program. This will solve the 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 scripts 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.
Yesterday I wanted to write a piece of code for downloading a file using PHP. I didn't want to set the HTTP protocol, so I went directly to php.net to find the example of the header () function. I copied a lot of code,
$ File = 'filetest.txt '; // you can simply write something into the filetest.txt file.
Header ("content-Disposition: attachment; filename =". urlencode ($ file ));
Header ("Content-Type: Application/force-download ");
Header ("Content-Type: Application/octet-stream ");
Header ("Content-Type: Application/download ");
Header ("content-Description: File Transfer ");
Header ("Content-Length:". filesize('filetest.txt '));
Flush (); // This doesn' t really matter.
$ Fp = fopen ($ file, "R ");
While (! Feof ($ FP ))
{
Echo fread ($ FP, 65536 );
Flush (); // This is essential for large downloads
}
Fclose ($ FP );
?>
When I run the command, I find that it is not working. The following error occurs: Warning: cannot modify header information-headers already sent by (output started at E:/XAMPP/htdocs/test/downloadfile/file_download.php: 1) in E:/XAMPP/htdocs/test/downloadfile/file_download.php on line 3
I have been reading it for a long time. The file is directly the header code at the beginning. How can I say that there is no output? After I found the prompt from someone else on the internet, I found that when I created the file, I stored it as utf8 directly using notepad. This would also lead to errors.
---------------- The following are suggestions for referencing others --------------------
Method 1:
There are some restrictions on the use of cookies in PHP.
1. Use setcookie before the tag
2. You cannot use echo to input content before using setcookie.
3. The cookie will not appear until the webpage is loaded
4. setcookie is sent only when it is placed in any data output browser.
.....
Due to the above restrictions, when using the setcookie () function, you will encounter "undefined Index", "cannot modify header information-headers already sent "... The solution is to generate a cookie before the output content. You can add the function ob_start () at the top of the program ();
Ob_start: Open the output buffer.
Function Format: void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.
Method 2:
Warning: cannot modify header information-headers already sent ...... A few days ago, I installed a PHP Big Head sticker system test and found the error warning: cannot modify header information-headers already sent ......
This problem still occurs when openads is installed again today. Angry. After searching for the Internet for a long time, someone said they would write it at the beginning of the file.
Ob_start ();
Failed.
Then open PHP. ini and set output_buffering to on. Restart appache, OK. It seems that this is the solution.
Special note: (I just read this to Solve the Problem)
If you use UTF-8 encoding, be sure to remove the BOM in the UTF-8, this is because the UTF-8 encoding file contains the BOM, and PhP4, 5 are not supported Bom. Remove BOM and use Notepad ++ to open the conversion. (I just read this to Solve the Problem)
Use PHP's ob_start (); to control your browser cache.

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.