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:
- 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
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.