This article introduces how to resolve Warning: cannot modify header information-headers already sent by in php when headers cannot be redirected ......
The following message is often displayed: 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 whether 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 setcookie must be placed inBefore Mark
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 use PHP to write a piece of code for downloading files, because I didn't want to set up the HTTP protocol to directly go to php. NET to find the header () function example, a lot of code, I copied a paragraph directly,
$ 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. to use setcookie, you mustBefore 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. I have also reproduced an article about using PHP's ob_start () to control your browser cache.
For more articles about the inability to redirect headers in php, refer to the PHP Chinese website!