How to solve the problem that PHP cannot modify header information_PHP tutorial

WBOY
Release: 2016-07-15 13:35:07
Original
1072 people have browsed it

In actual use

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

Warning: Cannot modify header information - headers already sent by

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

Warning: header information cannot be modified - headers have already been sent. . .

So what is the reason for this PHP cannot modify header information warning?

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

  1. < ?PHP
  2. echo "hello";
  3. header("content-type:
    text/html;charset:utf-8");
  4. ?>

A warning will appear in the above code!

Why is there a warning error if there is any output before header and setcookie? It's easy to understand if you understand the processing process of PHP!

Then how does PHP deal with the fact that PHP cannot modify the 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 (i.e. the main content in the http protocol). This is impossible if you The header information that has been sent has been modified in any way, so it is impossible for us to do anything using functions such as header and setcookie that modify headers!

Then let’s solve the problem that PHP cannot modify the header information!

The first method is very simple! Just try to avoid any output content before header and setcookie. Try to write them first.

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

There are two methods. One is to enable outbuffer in PHP.ini. The default value of output_buffering is 0. It 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 specified value. Maximum number of bytes (e.g. output_buffering=4096).

Another method in which PHP cannot modify header information is to enable it in a PHP script:

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

In this way, we have enabled PHP's output buffering and we can do any operation

<ol class="dp-xml"><li class="alt">
<span class="tag"><</span> ?PHP   </li><li><span>ob_start();  </span></li><li class="alt"><span>echo "dfdfd";  </span></li><li><span>//注意不能卸载ob_start() 前面  </span></li><li class="alt"><span>header("content-type:text/<br />html;</span><span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8");  </span></li><li><span>setcookie();  </span></li><li class="alt"><span class="tag">?></span><span> </span>
</li></ol>
Copy after login

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

There are some functions about outbuffer:

ob_flush()
Send output buffer (output buffer)

ob_end_flush()
Send output buffer and disable output buffering mechanism.

ob_end_clean()
Clear the output buffer without sending it, and disable output buffering.

ob_get_contents()
Return the current output buffer into a string. Allows you to process any output emitted by the script.

ob_get_clean()
Returns the current output buffer into a string. Allows you to process any output emitted by the script and disable the output buffering mechanism.

There are also some functions that can be found in the PHP manual. Search ob_

<ol class="dp-xml"><li class="alt">
<span class="tag"><</span> ?PHP  </li><li><span>ob_start();  </span></li><li class="alt"><span>print "Here's a pretty dumb way <br />to calculate the length of a string.";  </span></li><li><span>$</span><span class="attribute">length</span><span> = </span><span class="attribute-value">strlen</span><span>(ob_get_content());  </span></li><li class="alt"><span>ob_end_clean();  </span></li><li><span class="tag">?></span><span> </span>
</li></ol>
Copy after login

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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445932.htmlTechArticleIn actual use, for example, the following error warning that PHP cannot modify header information: Warning: Cannot modify header information - headers already sent by We just started writing PHP programs...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template