Completely eliminate PHP session cookie error

Source: Internet
Author: User
Tags ini php file php session php code php script setcookie

This 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 not warning it. We're going to take care of it today .....

Read 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 ... ”。

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

Do not use the above function before there are any text, blank lines, carriage returns, spaces and so on. But... The problem is that the answer is not satisfying. Because often the program in other PHP environment is normal operation.

First of all: How did this error arise? 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 (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 will allow you to modify the header information, including the default headings (such as content-type headings). However, once the script sends any non-caption output (for example, using HTML or print () calls), PHP must first send all headers and then terminate the HTTP header. And then continue sending the principal data. From this point on, any attempt to add or modify header information is not allowed and will send one of the above error messages.

Well, then we'll fix it:

Stupid method: Do not show the error warning all!
The concrete method does not say the plan ^_^

Solution:

1 is suitable for editing PHP with permission. INI's People

Open PHP. INI file (you try to test your PHP better than I do). Where is the INI), find

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

2 Use a virtual host, you cannot edit PHP. INI, how to do?

Simple:

Create one in the root directory of your space. htaccess file, which reads as follows:

AllowOverride All
Php_flag output_buffering on

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

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

3 in the PHP file to solve

Ob_start ()
Enable the output buffering mechanism. Output Buffering supports multiple tiers-for example, the Ob_start () function can be called multiple times.

Ob_end_flush ()
Sends output buffer (output buffering) and disables 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 to a string. I promise you to handle any output that the script emits.

Principle:

When output_buffering is enabled, PHP does not send HTTP headers when the script sends output. Instead, it enters the output through the pipeline (pipe) into the dynamically incremented cache (only PHP 4). 0, it has a central output mechanism. You can still modify/add headers, or set cookies, because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser and then sends the contents of the output buffer.

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.