Header in PHP cannot jump

Source: Internet
Author: User
Tags setcookie
Fix Warning:cannot Modify header information-headers already sent by ...

Information often prompts: Cannot modify header information-headers already sent by (...). In fact, has achieved the desired effect, is this error message look uncomfortable, online find a lot of ways, comprehensive use to get the solution is

1 Add Ob_start () to the PHP tag at the top of the page;

2 Add Ob_end_flush () below the information returned;

So we can block the reality of the error message.

Another way to turn other people, maybe in other cases will also be effective

If you got the message: "Warning:cannot Modify header Information-headers already sent by ..."
If you see this warning when you execute a PHP program: "Warning:cannot Modify header Information-headers already sent by ..."

Few notes based on the following user posts:
The following workarounds are available:

1. Blank lines (empty line):
Make sure no blank line after <?php ...? > of the calling PHP script.
Check for <?php ...? There are no blank lines behind >, especially the Include or require files. Many problems are caused by these blank lines.

2. Use Exit statement (to be resolved with exit):
Use exit after header statement seems to help some people
Add exit () after the header;
Header ("location:xxx");
Exit ();

3. PHP have this annoying problem, if your HTML goes before any PHP code or any headers modification before redirecting to C Ertain page, it ' ll said ' Warning:cannot Modify header information-headers already sent by .... Basically anytime you output to browser, the header was set and cannot be modified. So-ways to get around the problem:

3a. Use JavaScript (to be solved with JavaScript):

<? echo "<script> self.location (\" File.php\ ");</script>";?>

Since it ' s a script, it won ' t modify the header until execution of Javascript.
You can use JavaScript instead of headers. But the above code I did not execute success ... It is also important to note that this approach requires JavaScript to be supported by the browser.

3b. Use output buffering (to be resolved with the export cache):

<?php Ob_start ();?> ... HTML codes ... <?php ... PHP codes ... header ("Location: ...."); Ob_end_flush ();?>

This would save the output buffer on server and not output to browser yet, which means you can modify the header all you WA NT until the Ob_end_flush () statement. This method is cleaner than the JavaScript since JavaScript method assumes the browser have JavaScript turn on. However, there is overhead to store output buffer in server before output, but with modern hardware I would imagine it wo N ' t be the that big of deal. JavaScript solution would is better if you know for sure your user have Javascript turn on their browser.

Like the code above, this method caches the page when it is generated, allowing the header to be output after the head is output. The wish Board of this site is to solve the problem of the header with this method.
In the background management or sometimes in the forum, click on a page, the top of the page will appear
Warning:cannot Modify header Information-headers already sent by ....
This type of statement causes the problem because of the Setcookie statement.

The cookie itself has some limitations on its use, such as:
1. The setcookie of the call must be placed before the 2. Do not use the echo before calling Setcookie
3. Cookies will not appear in the program until the Web is re-loaded
4.setcookie functions must be sent out before any information is delivered to the browser.
5 .....
Based on these limitations, the Setcookie () function is often met with "Undefined index", "Cannot modify header Information-headers already sent by" ... And so on, solving the "cannot modify header Information-headers already sent by" This is the wrong way to produce a cookie before the cache input to the browser, so you can add ob_ in front of the program Start (); this function. This will solve the problem.

4.set output_buffering = on php.ini (opens output_buffering in php.ini)
Set output_buffering = On would enable output buffering for all files. But this method could slow down your PHP output. The performance of this method is depends on which Web server as you ' re working with, and what kind of the scripts you ' re using.
This method is theoretically the same as the 3b method. However, this approach opens the output cache for all PHP programs, which can affect the efficiency of PHP execution, depending on the performance of the server and the complexity of the code.

Yesterday want to use PHP to write a download file code, because do not want to set the HTTP protocol directly to Php.net to find the header () function case, a lot of code, I directly copied a paragraph,

<?php $file = ' filetest.txt ';//filetest.txt file you can just write something and get in there. 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 was essential for large downloads}fclose ($fp);? >

Run a bit found no, always error: 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 looked very long, the file is directly from the beginning of the header code, no output how can say already have character output? Later on the internet to find someone to give the hint, only to discover, originally I create the file is directly with Notepad storage as UTF8, originally this will also error

----------------below are suggestions for referencing others--------------------

Method One:
There are some restrictions on the use of cookies in PHP.
1, the use of Setcookie must be before the 2. You cannot use the Echo input before using Setcookie
3. Cookies will not appear until the page is loaded
4, Setcookie must be placed in any data output browser before sending out
.....
Due to the above limitations, when using the Setcookie () function, learn to encounter "Undefined index", "Cannot modify header Information-headers already sent by" ... The solution is to generate a cookie before outputting the content, and the function Ob_start () can be added at the top of the program;

Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.


Method Two:
Fix Warning:cannot Modify header information-headers already sent by ... A few days ago loaded a PHP paste system test, found error Warning:cannot modify header information-headers already sent by ...
Today, the openads, or the problem arises. Angry. On the internet for a long while, some people say to write at the beginning of the document
Ob_start ();
Failed.
Then open the php.ini and set the output_buffering to ON. Re-starting Appache,ok. It seems that this is the solution.

Special attention: (I just saw 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 encoded file contains the BOM reason, and php4,5 is not support BOM. Remove the BOM, you can open the conversion with notepad++. (I just saw this to solve the problem)

Use PHP's Ob_start (); Control your browser cache. I have separately reproduced an article about using PHP Ob_start (); Control your browser cache article

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.