In PHP, the cookie learns the warning: cannot modify header information-headers already sent by error.

Source: Internet
Author: User

Reference

Http://www.cnblogs.com/panjun-Donet/articles/1250405.html

Http://www.jb51.net/article/14108.htm

Obviously, this is because of setcookie. I checked the Internet and explained the following: the cookie itself has some restrictions, for example:

1. The description of the call to setcookie must be placed before the <HTML> tag

2. You cannot use echo before calling setcookie.

3. The cookie will not appear in the program until the website is reinjected.

4. The setcookie function must be sent out before any information is sent to the browser.

5 .......

Based on the above restrictions, when setting the setcookie () function, we often encounter "undefined Index", "cannot modify header information-headers already sent "... To solve the problem, the solution "cannot modify header information-headers already sent by" is to delay the data output to the browser before the cookie is generated. Therefore, you can add ob_start (); at the beginning of the program. This will solve the problem. If you want to add ob_start (), it is not feasible. It seems a little depressing that the program has been written ..
When I found that this error was prompted, I was wondering why I didn't prompt this problem locally. I thought it was a PHP. ini configuration that was different. I thought it was similar ..

So let's look at the sentence "output started .... "It means that the output is already in another place before setcookie, so find the file following output started at and see that the first line is blank, and then <? Is PHP a problem in this line ?? I also have this line locally. Remove it, refresh it, and try again. Well, it is here, and no longer prompts warning.

Solution complete!
Solution 2:

There are many ways to solve this problem on the Internet, but today we have encountered this problem. After the test, we found it feasible:

Find the configuration file PHP. ini in Windows on drive C, and then find the item: output_buffering, change its value from the original off to on, and restart Apache.

I solved the problem by solving method 2.

<? PHP if (! Empty ($ _ cookie ['counter']) {$ counter = $ _ cookie ['counter'] + 1; echo "you are the first ". $ counter. "A visitor ". '<br>';} else {$ counter = 1;} setcookie ("counter", $ counter, time () + 2678400 ); // calculate the access traffic for one month?>

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.