Prevent PHP session, cookie, and cannot modify header errors.

Source: Internet
Author: User
Tags php session
This article discusses how to completely eliminate warning: cannot add header information-headers already sent in ...... This is a confusing error.

As long as you have written PHPCodeI believe that we have met this extremely confusing warning. Today we will fix it ...............
After reading the PHP manual, the answer is as follows:
Message "Warning: cannot send session cookie-headers already sent ..." Or "cannot add/modify header information-headers already sent ...".

The function header (), setcookie (), and session functions must add header information to the output stream. However, the header information can only be sent before any other output content. Before using these functions, there cannot be any (such as HTML) output. The headers_sent () function can check whether your script has sent the header information. See "output control functions ".

Do not use any text, blank lines, carriage return, or spaces before using the above function. But... The question is, this answer is not satisfactory. BecauseProgramIt runs normally in other PHP environments.

First, how is this error generated? Let's take a look at how PHP handles HTTP header output and body output.

When the PHP script starts to be executed, it can send both header and subject information. Header information (From header () or setcookie () function) is not sent immediately. Instead, it is saved to a list. This allows you to modify the title information, including the default title (for example, Content-Type title ). However, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate the HTTP header. Then send the subject data. At this time, any attempt to add or modify the header information is not allowed, and one of the above error messages will be sent.

Good! Let's solve it: stupid method: Do not display all error warnings! Measure

Error_reporting (e_error | e_parse); Do not display e_warning here.

Solution:

1) applicable to PhP editing with permissions. INI user
Open PHP. INI file (You Should Know Your PHP better than me. INI), find
Output_buffering = changed to on or any number. If it is IIS6, change it to on. Otherwise, your PHP efficiency will be very slow.

2) use a VM and cannot edit PHP. INI, what should I do?

Simple:
Create one under the root directory of your space. The content of the htaccess file is as follows:

AllowOverride all
Php_flag output_buffering on

Unfortunately: or not? Cannot all webpages be displayed?

Then, you can call a Space Provider and ask him to call Apache. Htaccess AllowOverride Enabled

3) solve the problem in the 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 ()
Sends the output buffer and disables the output buffering mechanism.

Ob_end_clean ()
Clear the output buffer but not send it, and disable output buffering.

Ob_get_contents ()
Returns the current output buffer as a string. Allows you to process any output from the script.

Principle:
When output_buffering is enabled, PHP does not send HTTP headers when the script sends the output. Instead, it inputs this output through the pipeline (PIPE) to the dynamically added cache (only in PHP 4. 0, which has a central output mechanism ). You can still modify/Add the header or set the cookie 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 content in 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.