Home > Backend Development > PHP Tutorial > Solution to PHP setcookie() cannot modify header information_PHP Tutorial

Solution to PHP setcookie() cannot modify header information_PHP Tutorial

WBOY
Release: 2016-07-21 15:48:00
Original
937 people have browsed it

The following error is always reported when using the setcookie() function:
Warning: Cannot modify header information - headers already sent by....
The solution is as follows:
Method 1:
There are some restrictions on the use of cookies in PHP.
1. Use setcookie must be before the tag
2. Before using setcookie, you cannot use echo to enter content
3. The cookie will not appear until the web page is loaded
4 , setcookie must be placed before any data output browser before sending
...
Due to the above restrictions, when using the setcookie() function, you will encounter "Undefined index" and "Cannot modify header" Information - headers already sent by "... and other problems, the solution is to generate a cookie before outputting the content. You can add the function ob_start() at the top of the program;
ob_start: open the output buffer
Function format: void ob_start(void)
Note: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
Method 2:
Solving Warning: Cannot modify header information - headers already sent by ......
I installed a php photo post system a few days ago to test and found that Error Warning: Cannot modify header information - headers already sent by ......
I installed openads again today, and this problem still occurs. Angry. After searching online for a long time, someone said to write
ob_start();
at the beginning of the file but failed.
Later open php.ini and set output_buffering to on. Restart appache, OK. It seems this is the solution.
Special note:
If you use utf-8 encoding, you must remove the BOM in UTF-8. This is because the utf-8 encoded file contains BOM, and php4 and 5 do not support BOM. . To remove the BOM, you can open it with Notepad++ and convert it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319841.htmlTechArticleThe following error is always reported when using the setcookie() function: Warning: Cannot modify header information - headers already sent by. ... The solution is as follows: Method 1: The use of cookies in PHP is...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template