Solution to the problem that PHP cannot modify header information

不言
Release: 2023-04-03 10:58:01
Original
2385 people have browsed it

When using PHP, you may sometimes encounter some errors. How do these errors occur? And how do we solve these problems? This requires us to have a certain grasp of the language PHP. Now let me talk about how to solve the problem that PHP cannot modify header information.

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 Sometimes, you may have encountered this problem. Literally, it means:

Warning: Cannot modify header information - headers have already been sent. . .

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

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

< ?PHP echo "hello"; header("content-type: text/html;charset:utf-8"); ?>
Copy after login

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!

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

When the script has any output (first output) PHP will first send header information to the customer service end Then send the output content (that is, the main content in the http protocol). If you do not make any modifications to the header information that has been sent, it is impossible for us to use the header. And setcookie and other functions that modify the header can do anything!

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. After the program is executed, combine the header and outbuffer. and sent to the client.

There are two methods. One is to open 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 way that 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 the beginning of the public file;

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

< ?PHP ob_start(); echo "dfdfd"; //注意不能卸载ob_start() 前面 header("content-type:text/html;charset=utf-8"); setcookie(); ?>
Copy after login

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

About outbuffer also has some functions:

ob_flush()
Send output buffer (output buffer)

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

ob_end_clean()
Clear the output buffer but do not send 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()
Return 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_

< ?PHP ob_start(); print "Here's a pretty dumb way to calculate the length of a string."; $length = strlen(ob_get_content()); ob_end_clean(); ?>
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. It is not simply processed using the strlen() function, but is enabled first The output buffering mechanism prints the string and then determines the length of the output buffer. Finally clear the output buffer (not sent), and then disable the output buffering mechanism.

Related recommendations:

An exception occurred when modifying information in php, and the information cannot be modified

The above is the detailed content of Solution to the problem that PHP cannot modify header information. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!