setcookie Cannot modify header information headers already sent by

  • 2020-06-01 08:54:18
  • OfStack


<?php
   setcookie("username","bu",time()+3600);
   echo "aaaaa";
?>

Run with warning Warning: Cannot modify header information headers already sent by

Here are some Suggestions

Method 1: the use of Cookie in PHP is limited. 1. setcookie must be in < html > 3. cookie will not appear until the page has been loaded. 4. setcookie must be placed in front of any data output browser before it is sent out... Due to the above limitations, when using the setcookie() function, learn to encounter "Undefined index", "Cannot modify header information information headers sent sent by"... The solution is to generate cookie before the output content, and the function ob_start() can be added to the top of the program; Note: when the buffer is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. To output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer. Method 2:
Warning: Cannot modify header information-headers already sent by... A few days ago, I installed a test of php's headpost system, and found that the error was reported: Warning: Cannot modify header information-already sent by sent by Today, openads is installed again, but this problem still appears. Nu. After a lot of searching on the Internet, someone said they would write ob_start() at the beginning of the document. Failure. Then you open php.ini and you set output_buffering to on. Restart appache, OK. That seems to be the solution. Special note :(this is how I solved the problem.)
If utf-8 encoding is used, BOM in UTF-8 must be removed from UTF-8. This is because bom is included in utf-8 encoding file, while php4 and 5 do not support bom. Get rid of bom, you can use Notepad++ to turn on conversion 1. (that's how I solved the problem.)

Take care of yourself

PHP4,5 do not support BOM in the utf-8 encoding file

If the file contains BOM, an error will be reported when setting cookie or session. How to remove BOM files quickly and easily. Notepad++ is a very good free software. 1. It can be set in -- > Preferences - > New default new UTF-8 (no BOM)
2. If it is a new php file somewhere else, open how to convert the code to UTF-8 in notpad++ (no BOM) Format - > UTF-8 (no BOM) can now be transcoded.

Related articles: