php - setcookie 的问题

标签 php xampp

有一个想法,无论如何我都无法理解:((( 当我尝试设置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 。为了解决这个问题,您需要保存不带字节顺序标记的文件。

另请参阅:Byte order mark#Unwanted BOMs - Wikipedia

关于php - setcookie 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2446506/

相关文章:

php - require_once :failed to open stream: no such file or directory

php - 如何使用 localhost 或其他外部 IP

php - 连接到同一网络上另一台电脑上的 mysql 数据库

php - XAMPP phpMyAdmin session 错误

php - 追踪段错误 11 到大型 PHP 代码库

php - 验证多个复选框 laravel 5.2

linux - 无法从 Ubuntu 服务器上的外部站点连接到 phpMyAdmin

php - 如何同时使用xampp和mysql服务器

PHP ORDER BY 不工作嵌套查询

php - 实现订阅月份计数器的方法是什么?