Home >Backend Development >PHP Tutorial >A complete solution to the problem of Cannot modify header information in PHP
It is obvious that such a statement is caused by setcookie. After checking the Internet, there is the following explanation: Cookie itself has some restrictions on use, such as:
1. The description of calling setcookie must be placed in 2. Echo cannot be used before calling setcookie 3. The cookie will not appear in the program until the web page is reloaded 4. The setcookie function must be sent before any data is output to the browser
5.......
Based on the above restrictions, when executing the setcookie() function, you often encounter problems such as "Undefined index", "Cannot modify header information - headers already sent by"... etc. To solve the problem of "Cannot modify header information - headers already sent by"... The method for this error "sent by" is to delay the output of data to the browser before generating the cookie. Therefore, you can add the ob_start(); function at the front of the program. This will solve it. If you want to add ob_start(), it is not feasible. It seems a bit depressing to change this after the program has been written. When I found out that this error was prompted, I was wondering why my local computer did not prompt this problem. I thought it was The PHP.ini configuration is different, but it seems wrong when I think about it. They are almost the same.. So look at the sentence "output started at...." that follows, which means that there is output in another place before setcookie. So I found the file following output started at, and saw that the first line was blank, and then Solved!
Solution 2:
The methods to solve this problem found online are mostly the same, but today I encountered such a problem again. After trying it, I found that it works:
Find the php.ini configuration file in WINDOWS on the C drive, then search for an item: output_buffering, change its value from off to on, and restart Apache and it will be ok.
The above has introduced a complete solution to the problem of Cannot modify header information in PHP, including all aspects. I hope it will be helpful to friends who are interested in PHP tutorials.