Home  >  Article  >  Backend Development  >  Solution to the problem of Cannot modify header information (php)

Solution to the problem of Cannot modify header information (php)

WBOY
WBOYOriginal
2016-08-08 09:29:201040browse

I made a unified error prompt function. In the function execution, I first processed the error address and wrote it into a cookie to facilitate the user to jump directly to the page to be executed after logging in. However, I found that when testing on the server, it actually prompted Error that does not occur locally: Warning: Cannot modify header information - headers already sent by....
Such a statement, it is obvious that the cause of this is caused by setcookie. After checking the Internet, there is the following explanation: cookie itself There are some restrictions on use, such as:
1. The description of calling setcookie must be placed before the tag
2. Echo cannot be used before calling setcookie
3. The cookie will not be used until the web page is reloaded. Appears in the program
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 will often encounter "Undefined index", "Cannot Modify header information - headers already sent by"... and other problems. The way to solve the error "Cannot modify header information - headers already sent by" is to delay the data output to the browser before generating the cookie. Therefore, you can Add ob_start(); this function at the front. 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 I think it's wrong. 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, then search for an item: output_buffering, change its value from off to on, and restart Apache and it will be ok.

PS: Sometimes, when you create a php application in SAE, if your jump and setcookie are not supported, this will also happen. SAE development document specifications need to be followed.

The above introduces the solution to the problem of Cannot modify header information (php), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn