Home  >  Article  >  Backend Development  >  Completely eliminate PHP session cookie errors_PHP tutorial

Completely eliminate PHP session cookie errors_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:45:061063browse

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:
The 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 use 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 does this error occur? Let us 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 tell you the specific method ^_^#
Solution:
1) Applicable to 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) 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 scold the space provider, and then Ask him to give you apache. Open htaccess AllowOverride
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 without sending it, and disable output buffering.
 ob_get_contents()
  Return the current output buffer as 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320405.htmlTechArticleAs long as you have written PHP code, I believe you have encountered this inexplicable warning most of the time. Let's... let's get it done today............. After reading the PHP manual, the answer is as follows: Message "...
Statement:
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