The following error is always reported when using the setcookie() function:
Warning: Cannot modify header information - headers already sent by....
The solution is as follows:
Method 1:
There are some restrictions on the use of cookies in PHP.
1. Use setcookie must be before the tag
2. Before using setcookie, you cannot use echo to enter content
3. The cookie will not appear until the web page is loaded
4 , setcookie must be placed before any data output browser before sending
...
Due to the above restrictions, when using the setcookie() function, you will encounter "Undefined index" and "Cannot modify header" Information - headers already sent by "... and other problems, the solution is to generate a cookie before outputting the content. You can add the function ob_start() at the top of the program;
ob_start: open the output buffer
Function format: void ob_start(void)
Note: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
Method 2:
Solving Warning: Cannot modify header information - headers already sent by ......
I installed a php photo post system a few days ago to test and found that Error Warning: Cannot modify header information - headers already sent by ......
I installed openads again today, and this problem still occurs. Angry. After searching online for a long time, someone said to write
ob_start();
at the beginning of the file but failed.
Later open php.ini and set output_buffering to on. Restart appache, OK. It seems this is the solution.
Special note:
If you use utf-8 encoding, you must remove the BOM in UTF-8. This is because the utf-8 encoded file contains BOM, and php4 and 5 do not support BOM. . To remove the BOM, you can open it with Notepad++ and convert it.