PhpCannotmodifyheaderinformation-headersalreadysentby solution

Source: Internet
Author: User
When I used cookies yesterday, the page reported an error: phpCannotmodifyheaderinformation-headersalreadysentby. next I will summarize the solution to the error.

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

  1. Ob_start ();
  2. Setcookie ("username", "bin", time () + 3600 );
  3. Echo "the username is:". $ HTTP_COOKIE_VARS ["username"]. "n ";
  4. Echo "the username is:". $ _ COOKIE ["username"]. "n ";
  5. Print_r ($ _ COOKIE );
  6. ?>

Warning: Cannot modify header information-headers already sent

The cause of the error is added to 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. to use setcookie, you mustBefore tag

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, we can shield the reality of the error information, but some friends say that the above method is not good. later I opened php. ini and set output_buffering to on, re-initiated appache, OK. It seems that this is the solution, another friend said that the encoding problem, 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.