Php Cannot modify header information-headers already sent

Source: Internet
Author: User

When I used cookies yesterday, the page reported the following error: php Cannot modify header information-headers already sent by. Next I will summarize the solution to the error.

Code

The Code is as follows: Copy code
<? Php
Ob_start ();
Setcookie ("username", "bin", time () + 3600 );
Echo "the username is:". $ HTTP_COOKIE_VARS ["username"]. "n ";
Echo "the username is:". $ _ COOKIE ["username"]. "n ";
Print_r ($ _ COOKIE );
?>

Warning: Cannot modify header information-headers already sent by error cause

I added the header of the php program,

Header ("cache-control: no-cache, must-revalidate ");

Cause Analysis

There are some restrictions on the use of cookies in PHP.
1. Use setcookie before the 2. You cannot use echo to input content before using setcookie.
3. The cookie will not appear until the webpage is loaded
4. setcookie is sent only when it is placed in any data output browser.
.....
Due to the above restrictions, when using the setcookie () function, you will encounter "Undefined index", "Cannot modify header information-headers already sent "... The solution is to generate a cookie before the output content.

Solution

1. Add ob_start () to the php tag at the top of the page ();

2. Add ob_end_flush () to the returned information ();

In this way, the error information can be blocked.


Some friends said that the above method was not feasible. Later I opened php. ini and set output_buffering to on. Restart appache, OK. It seems that this is the solution.


Another friend said that encoding problems, as long as the conversion document has a uft-8 can solve the problem, here is not introduced.

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.