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.