Setcookie cannot modify header information-headers already s_php tutorial

Source: Internet
Author: User
Tags setcookie
Copy CodeThe code is as follows:
Setcookie ("username", "bu", Time () +3600);
echo "AAAAA";
?>

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

Here's what others suggest

Method One:
There are some restrictions on the use of cookies in PHP.
1, the use of Setcookie must be before the label
2. You cannot 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 "Undefined index", "Cannot modify header Information-headers already sent by" ... The solution is to generate a cookie before outputting the content, and the function Ob_start () can be added at the top of the program;

ob_start: Open Output Buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.


Method Two:
Fix Warning:cannot Modify header information-headers already sent by ... A few days ago loaded a PHP paste system test, found error Warning:cannot modify header information-headers already sent by ...
today, the openads, or the problem arises. Angry. On the internet for a long while, some people say to write at the beginning of the document
Ob_start ();
failed.
then open the php.ini and set the output_buffering to ON. Re-starting Appache,ok. It seems that this is the solution.

Special attention:(I just saw 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 encoded file contains the BOM reason, and php4,5 is not support BOM. Remove the BOM, you can open the conversion with notepad++. (I just saw this to solve the problem)

Own solution

php4,5 does not support the BOM in Utf-8 encoded files

If the file contains a BOM, setting a cookie or session will be an error. How to get rid of the BOM of the file quickly and conveniently. notepad++ software is recommended, very good free software. 1. You can set the new default new UTF-8 ( No BOM)
2. If a new PHP file is created elsewhere, open in notpad++ How to convert the encoding to UTF-8 (no BOM) format-->utf-8 (no BOM) Then you can transcode it.

http://www.bkjia.com/PHPjc/327005.html www.bkjia.com true http://www.bkjia.com/PHPjc/327005.html techarticle Copy the code as follows: PHP Setcookie ("username", "bu", Time () +3600); echo "AAAAA"; Run with warning Warning:cannot Modify header Informati On-headers already sent by ...

  • Related Article

    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.