Completely eliminate PHP session cookie errors

Source: Internet
Author: User
Tags php session

As long as you have written PHP code, I believe you have met this extremely confusing warning .. let's fix it today ...............
After reading 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 ...".
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. Because programs often run 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: Disable all error warnings!
The specific method is not to mention ^ _ ^ #
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.