Home > Backend Development > PHP Tutorial > setcookie中Cannot modify header information-headers already s_PHP教程

setcookie中Cannot modify header information-headers already s_PHP教程

WBOY
Release: 2016-07-21 15:10:52
Original
1030 people have browsed it

Copy code The code is as follows:

setcookie("username","bu",time()+ 3600);
echo "aaaaa";
?>

运行有警告Warning: Cannot modify header information - headers already sent by 

下面是别人建议

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 input content
3. The cookie will not appear until the web page is loaded
4. Setcookie must be placed before any data is output to the browser before it is sent
... ..
Due to the above restrictions, when using the setcookie() function, you may encounter problems such as "Undefined index", "Cannot modify header information - headers already sent by"..., the solution is Before outputting content, a cookie is generated. 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)
Explanation: 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:
Solution to Warning: Cannot modify header information - headers already sent by ..... . A few days ago, I installed a php photo sticker system to test, and found that the error message Warning: Cannot modify header information - headers already sent by ......
I installed openads again today, and this error still occurred. question. 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: (I solved the problem just after reading this)
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. (I solved the problem after reading this)

Solution by yourself

PHP4 and 5 do not support BOM in utf-8 encoded files

If the file contains BOM, an error will be reported when setting cookie or session.
How to quickly and easily remove the BOM of a file.
It is recommended to use Notepad++ software, a very excellent free software.
1. You can create a new UTF-8 (no BOM) in Settings-->Preferences-->New by default

2. If it is a newly created php file elsewhere, open in notpad++ how to convert the encoding to UTF-8 (no BOM)
Format-->UTF-8 (no BOM) Now you can transcode.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327005.htmlTechArticleCopy the code as follows: ?php setcookie("username","bu",time()+3600); echo "aaaaa"; ? There is a warning when running. Warning: Cannot modify header information - headers already sent by The following 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