有一个想法,无论如何我都无法理解:((( 当我尝试设置cookie(位于login.php中的第28行)时,浏览器向我返回错误!
Cannot modify header information - headers already sent by (output started at C:\xampp2\htdocs\video\index.php:9) in C:\xampp2\htdocs\video\login.php on line 28
但是在索引 php 的第 9 行,我没有任何 header !!!有标签!!!
我听不懂!有人能告诉我为什么它会返回这样的错误吗?
最佳答案
Cookie 作为 header 发送。来自 setcookie 的 PHP 文档:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.
此外,如果您的页面以 UTF-8 格式保存,BOM(字节顺序标记)将阻止您设置任何 header ,因为它算作输出。请参阅http://bugs.php.net/bug.php?id=22108 。为了解决这个问题,您需要保存不带字节顺序标记的文件。
关于php - setcookie 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2446506/