How to solve the Cannot modify header information-headers already sent by error in setcookie

Source: Internet
Author: User

Copy codeThe Code is as follows: <? Php
Setcookie ("username", "bu", time () + 3600 );
Echo "aaaaa ";
?>

Run Warning: Cannot modify header information-headers already sent

Below are suggestions from others

Method 1:
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. 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 zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.


Method 2:
Solve Warning: Cannot modify header information-headers already sent ...... A few days ago, I installed a php Big Head sticker system test and found the error Warning: Cannot modify header information-headers already sent ......
This problem still occurs when openads is installed again today. Angry. After searching for the Internet for a long time, someone said they would write it at the beginning of the file.
Ob_start ();
Failed.
Then open php. ini and set output_buffering to on. Restart appache, OK. It seems that this is the solution.

Special note: (I just read this to Solve the Problem)
If you use UTF-8 encoding, be sure to remove the BOM in the UTF-8, this is because the UTF-8 encoding file contains the bom, and php4, 5 are not supported bom. Remove bom and use Notepad ++ to open the conversion. (I just read this to Solve the Problem)

Solution

PHP4 and 5 do not support BOM in UTF-8 encoded files

If the file contains BOM, an error is reported when the cookie or session is set. how to quickly and conveniently remove the BOM of a file. we recommend that you use Notepad ++, a very good free software. 1. you can create a new UTF-8 (no BOM) by default in settings --> preferences --> New) 2. If it is a PHP file created elsewhere, open in notpad ++ how to convert encoding into UTF-8 (without BOM) format --> UTF-8 (without BOM) Transcoding is now available.

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.