PHP cannot modify header information solution

Source: Internet
Author: User
Tags setcookie

When using PHP, there are times when you may encounter some errors, how are these errors generated? And how do we solve these problems? This requires that we have a certain grasp of the language of PHP. Let me talk about how to solve the problem that PHP cannot modify header information.

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

Warning:cannot Modify header Information-headers already sent by

We've probably had this problem when we started writing PHP programs. From the literal point of view, it means:

Warning: Cannot modify header information-headers has been sent ...

So what is the reason why this PHP cannot modify the header information warning!

is when we have output before the header () or Setcookie () function, such as:

<? PHP  echo "Hello";  Header ("Content-type:    text/html;charset:utf-8");  ? >

The above code will show a warning!

Why is there a warning error in any output before the header and Setcookie? If you understand the process of PHP is easy to understand!

What does PHP do with PHP that cannot modify header information?

When the script has any output (the first output) PHP will first send header information to the customer service and then send the output, (that is, the main content of the HTTP protocol) This is if you can not make any changes to the header information has been sent, so we can not use the header and Setcookie and so on to modify the function of the header to do anything!

Well, let's do it. If PHP cannot modify the header information This problem!

The first method is simple! is to try to avoid any output before the header and Setcookie. Try to write them in front.

The second solution is to use the PHP outbuffer output buffer, PHP output buffer is such, the current script of all the output is put into the Outbuffer, when the program is completed after the header and outbuffer a concurrent send to the client.

There are two ways to turn on Outbuffer in php.ini output_buffering The default value of 0 can be set to off or on if you want to limit the maximum value of the output buffer, you can set this option to the maximum number of bytes specified (for example, Output_ buffering=4096).

Another way that PHP cannot modify header information is to open it in a PHP script:

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

So we can start the PHP output buffer and we'll do anything.

<? PHP   Ob_start ();  echo "DFDFD";  Note You cannot uninstall the Ob_start () Front  header ("Content-type:text/html;charset=utf-8");  Setcookie ();  ? >

If you want to start gzip you can add Ob_gzhandler callback function Ob_start ("Ob_gzhandler") for Ob_start;

There are also some functions about Outbuffer:

Ob_flush ()
Send output buffer (outputs buffered)

Ob_end_flush ()
Send output buffer (out buffering) and disable the output buffering mechanism.

Ob_end_clean ()
Clears output buffer but does not send, and disables output buffering.

Ob_get_contents ()
Returns the current output buffer as a string. Allows you to handle any output that the script emits.

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

There are also some functions that can be consulted in PHP manual search Ob_

<? PHP  Ob_start ();  Print "Here's a pretty dumb to calculate the length of a string.";  $length = strlen (Ob_get_content ());  Ob_end_clean ();  ? >

This PHP cannot modify the header information example shows a very inefficient way to determine the length of a string. Instead of simply using the strlen () function, it first enables the output buffering mechanism, prints the string, and then determines the length of the output buffer. Finally clear output buffer (not sent), and then 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.