SoFunction
Updated on 2025-03-09

Completely eliminate PHP session cookie errors

As long as you have written PHP code, I believe you have encountered this warning that is often inexplicable... Today we will get it done...
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. However, the header information can only be sent before any other output content. There cannot be any output (such as HTML) before using these functions. The function headers_sent() can check whether your script has sent header information. See "Output Control Functions".
Meaning: Don't have any text, blank lines, carriage return, spaces, etc. before using the above function. but. . . The problem is, this answer is not satisfactory. Because the program often runs normally in other PHP environments.
First of all: How does this error occur? Let's see how PHP handles HTTP header output and body output.
When the PHP script starts executing, it can send header (title) information and body information at the same time. The header information (from the header() or SetCookie() function) is not sent immediately, instead it is saved to a list. This allows you to modify the title information, including the default title (such as Content-Type title). However, once the script sends any non-titled output (for example, using HTML or print() calls), PHP must first send all headers and then terminate the HTTP header. Then continue to send the subject data. From this point on, any attempt to add or modify the header information is not allowed and one of the above error messages will be sent.
OK! Then let's solve it:
Stupid method: Don't display any error warnings!
I won’t talk about the specific method of covering up my ears and stealing the bell ^_^#
Solution:
1) Applicable to edit PHP with permission. INI people
Open php. Ini file (you are more clear to your php than me. 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 will be extremely slow.
2) When using a virtual host, you cannot edit PHP. INI, what to 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
Unfortunately, is it: Or is it not possible? All web pages cannot be displayed?
Then, you can call and scold the space quotient and ask him to give you apache. htaccess AllowOverride opens
3) Solve it in PHP file
ob_start()
Enable the output buffering mechanism. Output buffering supports multi-level -- for example, the ob_start() function can be called multiple times.
ob_end_flush()
Send an output buffer and disable 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 to a string. Allows you to process any output emitted by the script.
principle:
When output_buffering is enabled, PHP does not send HTTP header when the script sends output. Instead, it pipes this output into a dynamically increased cache (only used in PHP 4.0, it has a centralized output mechanism). You can still modify/add the header, or set the cookie because the header is not actually sent. When all scripts terminate, PHP will automatically send HTTP header to the browser, and then send the content in the output buffer.