How to solve the problem of cannot modify header information in PHP

Source: Internet
Author: User
Tags header ini modify setcookie

When you do a project, you need to prepare a unified error prompt function. Small series in function execution, first deal with the wrong address to write cookies to facilitate the user login can jump directly to the implementation of this page, but found in the server test, unexpectedly prompts local error: Warning: Cannot modify header information-headers already sent by .... Such a statement, it is obvious that this reason is caused by setcookie, look up the internet, like the explanation: the cookie itself in the use of a number of restrictions, such as:  
1. Syria of call Setcookie must be placed in 2. Before calling Setcookie, you cannot use echo 
3. Cookies will not appear in the program until the web is uploaded  
4. The Setcookie function must send   before any data is output to the browser.

is based on these limitations, so when you perform the Setcookie () function, you will often encounter the Undefined index, cannot modify header information-headers already sent by. To solve the problem, "cannot modify header Information-headers already sent by" is the wrong way to slow down the data output to the browser before creating cookies, so you can add ob_ to the front of the program Start (): this function. This will solve the problem.   If you want to add Ob_start (), it is not feasible, the procedures have been written, just to change this seems a bit depressing. Found that prompted this error, I was thinking about how the local did not prompt this question, but also thought that the php.ini configuration is not the same, think again is not the same. &NBSP
Then look at the following sentence "output started at ..." means that there is already an output in another place before Setcookie, and then find the file at the back of output started, see the first line is blank, and then the <?php start like this, is this the problem of this line?? I also have this line locally, remove, refresh and try again, oh, sure enough out here, no longer prompt warning.
Finish!


Workaround Two:
There are many ways to find solutions to this problem on the Internet, but today it is a problem. After a trial, it was found to be feasible:
Locate php.ini this profile in Windows C, and then look for an item: output_buffering change its value from the original off to on, and restart Apache OK.



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.