Warning:cannot send session Cookie–headers already sent ...

Source: Internet
Author: User

Believe that most people write PHP code, have encountered similar "Warning:cannot send session cookie–headers already sent ..." or "Cannot add/modify header infor Mation–headers already sent ... " Such warning hints. Let's take a look at the causes and solutions to this warning.

The function header (), Setcookie (), and session functions need to add header information to the output stream. But the header information can only be sent before any other output content. You cannot have any output (such as HTML) before you can use these functions. The function headers_sent () checks to see if your script has sent header information. See Output control functions.

Headers_sent-checks if or where headers have been sent.

The meaning is: Do not use the above function before any text, blank line, carriage return, space and so on. But... The problem is that the answer is not satisfactory. This is because the program runs normally in other PHP environments.

First of all: How did this error arise? Let's take a look at how PHP handles the HTTP header output and the main output.

When the PHP script starts executing, it can send header and principal information at the same time. 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 header information, including the default title (such as the Content-type title). However, once the script sends any non-header output (for example, using HTML, print () or Echo call), then PHP must first send all the headers and then terminate the HTTP header. Then continue to send the principal data. From this point on, any attempt to add or modify header information is not allowed, and one of the above error messages is sent.

Let's take a look at the solution:

1. Do not show the wrong warning, the way to cure the symptoms

Error_reporting (E_error | E_parse); Don't show e_warning here.

2. Edit PHP.ini

Open php.ini file and locate output_buffering = change to On or any number.

3. If you do not have permission to edit php.ini, such as using the virtual host

Build one in your space root. Htaccess file with the following content:

AllowOverride Allphp_flag output_buffering on

If not, then use the following method:

At the beginning of the PHP file, join: Ini_set ("output_buffering", "1″");

Let this page open the output cache of PHP.

4. Resolve in PHP file

Ob_start ()

Enable the output buffering mechanism. Output buffering supports multiple levels-for example, you can call the Ob_start () function multiple times.

Ob_end_flush ()

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

Ob_end_clean ()

Clears output buffer but does not send, and disables output buffering.

Ob_get_contents ()

Returns the current output buffer as a string. Allows you to handle any output that the script emits.

Principle:

When output_buffering is enabled, PHP does not send an HTTP header when the script sends the output. Instead, it imports this output through a pipeline (pipe) into a dynamically increasing cache (only used in PHP 4.0, which has a centralized output mechanism). You can still modify/add a header, or set a cookie because the header is not actually sent. When all the scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the contents of the output buffer.

5. If none of the above methods can wait for a satisfactory solution, please use the following method:

Use notepad++ and other editors to convert the file to UTF-8 without BOM-encoded file and try again.

the causes are mainly caused by the following two points:

One, the output of other content between the header () function (typically sent by the browser is hidden), resulting in the subsequent header not being able to send the new page type again. This can be solved by opening output_buffering, and 2,3,4 is the way it is.

Second, PHP files using UTF-8 encoding, because of incompatible coding (especially through other encoding conversion), produced a BOM "in the UCS code is called" ZERO WIDTH no-break SPACE "character, its encoding is Feff. Fffe is not a character in UCS, so it should not appear in the actual transmission. The UCS specification recommends that the character "ZERO WIDTH no-break SPACE" be transmitted before the byte stream is transmitted. This means that if the recipient receives Feff, the byte stream is Big-endian, and if Fffe is received, it indicates that the byte stream is Little-endian. So the character "ZERO WIDTH no-break SPACE" is also called a BOM.

The UTF-8 does not require a BOM to indicate byte order, but it can be used to indicate the encoding using a BOM. The UTF-8 code for the character "ZERO WIDTH no-break SPACE" is the EF BB BF. So if the receiver receives a byte stream beginning with the EF BB BF, it knows that this is UTF-8 encoded.

Windows uses a BOM to mark the way a text file is encoded. The resulting header file is not recognized correctly, at this time as long as the removal of the BOM in the UTF-8 file can be, Method 5 is based on this principle.

Warning:cannot send session Cookie–headers already sent ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.