Home >Backend Development >PHP Tutorial >Flash cookie completely eliminates PHP session cookie errors
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.............
Look Refer to the PHP manual and 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 cannot be any 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: How does 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.
Okay! Then let’s solve it:
Stupid method: Don’t display any error warnings!
I won’t go into the specific method to cover your ears and steal the bell ^_^#
Solution:
1) Suitable for those who have permission to edit PHP. INI people
Open php. ini file (you should know your php better than I do. Where is the ini), find
output_buffering = and change it to on or any number. If it is IIS6, please change it to ON, otherwise your PHP efficiency will be extremely slow.
2) When 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 working? All web pages cannot be displayed?
Then, you can call the space provider and ask him to install apache for you. htaccess AllowOverride is turned on
3) Solve it in the 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()
Return 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.
The above has introduced the flash cookie to completely eliminate PHP session cookie errors, including the content of flash cookies. I hope it will be helpful to friends who are interested in PHP tutorials.