Phpk empty row problem: solved warning: cannot modify header information-headers already sent...

Source: Internet
Author: User

From: http://zhanglulu33.blog.163.com/blog/static/112799542010112810497281/

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 <? PHP...?> Of the calling PHP script.
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 by... "basically anytime you output to browser, the header is set and cannot be modified. So two ways to get around the problem:

3A. Use javasUsing javas):

<? Echo "<SCRIPT> self. Location (\" file. php \ "); </SCRIPT>";?>

Since it's a script, it won't modify the header until execution of javasInvalid.
You can use javasToken to replace the header. However, the above Code is not successfully executed... In addition, you must note that using this method requires the browser to support javasInvalid.

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 javasCript since javasBytes
Method assumes the browser has javasPlease 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. javasBytes
Solution wocould be better if you know for sure your user has javasPlease 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. The description of the call to setcookie must be placed before the <HTML> 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 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,

<? PHP
$ 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) INE: \ 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 <HTML> 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 by... a PHP touchpost system test was installed a few days ago, and the error warning: cannot

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.