Setcookie cannot modify header Information-headers already sent by error resolution method _php Tips

Source: Internet
Author: User
Tags setcookie
Copy Code code as follows:

<?php
Setcookie ("username", "bu", Time () +3600);
echo "AAAAA";
?>

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

Here's what people suggest

Method One:
There are some limitations to the use of cookies in PHP.
1, use Setcookie must be before 2, can not use the Echo input before using Setcookie
3. Cookies will not appear until the page is loaded
4, Setcookie must be placed in any data output browser before sending out
.....
due to the above limitations, when using the Setcookie () function, learn to encounter the "Undefined index", "Cannot modify header information-headers already by" ... And so on, the solution is to output the content before the cookie, you can add a function at the top of the program Ob_start ();

ob_start: Open output buffer
function format: void Ob_start (void)
Description: When the buffer is activated, all non-file header information from the PHP program is not sent. Instead, it is saved in an 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 Two:
Resolve Warning:cannot Modify header Information-headers already sent by.. .... A few days ago installed a PHP sticker system test, found that the error Warning:cannot modify header information-headers already sent by ... The
is openads again today, or this problem occurs. Angry. On the internet for a long time, someone said to write the file at the beginning of the
Ob_start ();
failed. The
then opens the php.ini and then sets Output_buffering to ON. To start Appache,ok again. It seems that this is the solution.

Special Note: (This is what I see to solve the problem)
If you use UTF-8 encoding, be sure to remove the BOM in the UTF-8, because the Utf-8 encoding file contains the BOM reason, And php4,5 are not supported BOM. Remove the BOM, you can open the conversion with notepad++. (That's what I saw to solve the problem)

Own solutions

BOM in Utf-8 encoded file is not supported by php4,5

If the file contains a BOM, a cookie or session error will be provided.
How to quickly and easily remove the document BOM.
Recommend the use of notepad++ software, very good free software.
1. You can set the--> preferences--> new default new UTF-8 (no BOM)

2. If you are creating a new PHP file elsewhere, open how to convert the encoding to UTF-8 (no BOM) in notpad++
Format-->utf-8 (no BOM) This is the time to turn the code.

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.