How to solve the problem that PHP cannot modify header information

Source: Internet
Author: User
Tags php error php example

In actual use

For example, the following PHP error warning cannot modify the header information:

Warning: Cannot modify header information-headers already sent

We may have encountered this problem when we were writing PHP programs. Literally, it means:

Warning: the header information cannot be modified.-The headers has been sent...

So what is the cause of this warning that PHP cannot modify the header information!

That is, when we have output content before the header () or setcookie () function, for example:

  1. <? PHP
  2. Echo "hello ";
  3. Header ("content-type:
    Text/html; charset: UTF-8 ");
  4. ?> 

The above code generates a warning!

Why is there any output before the header and setcookie! If you understand the PHP processing process, you can easily understand it!

So how does PHP handle PHP's inability to modify header information?

When the script has any output (first output), PHP will first send the header information to the client and then send the output content (that is, the main content in the http protocol) this is because you cannot modify the sent header information. Therefore, we cannot use the header and setcookie functions to modify the header to do anything!

Let's solve the problem that PHP cannot modify the header information!

The first method is very simple! That is, try to avoid any output content before the header and setcookie. Try to write them in front.

The second solution is to use the outbuffer output buffer of PHP. The output buffer of PHP is like this. Put all the output content of the current script into the outbuffer, after the program is executed, the header and outbuffer are sent to the client.

There are two ways to do this: in PHP. enable outbuffer output_buffering in ini. The default value 0 can be set to Off or On. to limit the maximum value of the output buffer, you can set this option to the specified maximum number of bytes (for example, output_buffering = 4096 ).

Another way for PHP to modify the header information is to enable it in the PHP script:

Call the ob_start () function at the beginning of the program or the beginning of a public file ();

In this way, we can enable the PHP output buffer and perform any operations below.

 
 
  1. <? PHP
  2. Ob_start ();
  3. Echo "dfdfd ";
  4. // Note that You Cannot uninstall ob_start ().
  5. Header ("content-type: text/
    Html; charset = UTF-8 ");
  6. Setcookie ();
  7. ?>

If you want to start gzip, you can add the ob_gzhandler callback function ob_start ("ob_gzhandler") for ob_start ");

There are also some functions about outbuffer:

Ob_flush ()
Sending output buffer)

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.

Ob_get_clean ()
Returns the current output buffer as a string. Allows you to process any output from the script and disable the output buffering mechanism.

For more functions, refer to the PHP manual to search for ob _

 
 
  1. < ?PHP  
  2. ob_start();  
  3. print "Here's a pretty dumb way 
    to calculate the length of a string.";  
  4. $length = strlen(ob_get_content());  
  5. ob_end_clean();  
  6. ?> 

This PHP example of failing to modify the header information shows a very inefficient method for determining the string length. Instead of simply using the strlen () function, it first starts the output buffering mechanism, prints the string, and then determines the length of the output buffer. Finally, clear the output buffer (not sent) and disable the output buffering mechanism.


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.