Avoid PHP's session cookie error from the root _php tutorial

Source: Internet
Author: User
   Fire Building Station College (bkjia.com) network programmingThis article discusses how to completely eliminate the Warning:cannot add header information-headers already sent in .... This is a puzzling mistake.

As long as you have written PHP code, I believe you have encountered this most of the time is puzzling warning bar. Today we are going to take care of it .....

Read the PHP manual and answer the following:

Message "Warning:cannot send session Cookie-headers already sent ... "or" Cannot add header information-headers already sent ... ”。

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.

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 or print () calls), 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.

OK, let's fix it.

Stupid method: The error warning is not displayed!
Deceiving, the specific method will not say ^_^ #

Solution:

1) for PHP with permission to edit. INI of people

Open PHP. INI file (you are better than I know your PHP.) INI where), find

output_buffering = change to On or any number. If it is IIS6, please change to ON, otherwise your PHP efficiency will be very slow.

2) using a virtual host, you cannot edit PHP. INI, what to do?

Simple:

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

AllowOverride All
Php_flag output_buffering on

The unfortunate situation is: or not? All pages are not displayed?

Well, you can call and scold a space trader and then let him give you the Apache. htaccess allowoverride Open

3) Resolve 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 (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 the dynamically increasing cache (only in PHP 4. 0, it 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.

http://www.bkjia.com/PHPjc/364283.html www.bkjia.com true http://www.bkjia.com/PHPjc/364283.html techarticle Fire Building Station College (liehuo.net) network programming This article discusses how to completely eliminate Warning:cannot add header information-headers already sent in ... This is a puzzling mistake ...

  • 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.