Bkjia.Com Network ProgrammingThis article discusses how to completely eliminate warning: Cannot add header information - headers already sent in... This kind of annoying Inexplicable error.
As long as you have written PHP code, I believe you have encountered this warning that is puzzling most of the time.. Today we will solve it...... .
After reading the PHP manual, the answer is as follows:
Message "Warning: Cannot send session cookie - headers already sent..." or "Cannot add header information - headers already sent...".
The functions header(), setcookie() and session functions need to add header information to the output stream. But header information can only be sent before any other output content. There must be no output (such as HTML) before using these functions. The function headers_sent() checks whether your script has sent headers. See "Output Control Functions".
Meaning: Do not have any text, blank lines, carriage returns, spaces, etc. before using the above function. but. . . The problem is, this answer is unsatisfactory. Because often programs run normally in other PHP environments.
First of all: How did this error occur? Let's take a look at how PHP handles HTTP header output and body output.
When the PHP script starts executing, it can send header information and body information at the same time. Header information (from header() or SetCookie() function) is not sent immediately, instead, it is saved to a list. This allows you to modify the header information, including the default header (such as the Content-Type header). However, once the script sends any non-header output (for example, using HTML or a print() call), then PHP must first send all headers and then terminate the HTTP header. Then continue to send the main data. From this point on, any attempt to add or modify header information is not allowed and one of the above error messages will be sent.
OK! Let’s solve it:
Stupid method: Don’t display any error warnings!
I won’t tell you the specific method ^_^#
Solution:
1) Applicable to those who have permission to edit PHP. People from INI
Open php. ini file (you know your php better than I do. Where is the ini), find
Output_buffering =Change to on or any number. If it is IIS6, please change it to ON, otherwise your PHP efficiency will be extremely slow.
2) Using a virtual host, PHP cannot be edited. INI, what should I do?
Simple:
Create one in the root directory of your space. htaccess file, the content is as follows:
AllowOverride All
PHP_FLAG output_buffering On
The unfortunate situation is: Still not possible? All web pages cannot be displayed?
Then, you can call and scold the space provider, and then ask him to install apache for you. htaccess AllowOverride on
3) Solve it in PHP file
ob_start()
Enable the output buffering mechanism. Output buffering supports multiple levels - for example, the ob_start() function can be called multiple times.
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()
Returns the current output buffer into a string. Allows you to process any output emitted by the script.
Principle:
When output_buffering is enabled, PHP does not send HTTP headers when the script sends output. Instead, it pipes this output into a dynamically growing cache (only available in PHP 4.0, which has a centralized output mechanism). You can still modify/add headers, or set cookies, since headers are not actually sent. When all scripts terminate, PHP will automatically send HTTP headers to the browser, and then send the contents of the output buffer.