PHP 错误 "Can not modify header information"

标签 php html http-headers location

<分区>

我有这个错误,多年来我一直在努力修复它。它似乎是凭空出现的,因为它所在的页面昨天运行良好。

错误是:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/dev.racommerce.com/includes/header.php:97) in /var/www/dev.racommerce.com/cart.php on line 44

header.php文件中第97行的代码是:

<?for($i = 0; $i < $count; $i++) 
{?> 
  <li><a class="dropDown" href="<?="http://" . $_SERVER['HTTP_HOST'];?>/products.php?cat=<?=$result[$i]['categoryID']?>"><?=$result[$i]['categoryName']?></a></li> 
<?}?>

cart.php 文件第 44 行的代码是:

header("location: cart.php"); 

我一直在研究其他答案的解决方案,例如 How to fix "Headers already sent" error in PHP但我很难理解它们或在我的代码中实现它们。

有人可以帮我解决这个问题吗?

最佳答案

你不能在标题前放任何东西,比如 echo 或 close php 标签。如果放在开头就可以修复它

ob_start();

最后

ob_end_flush();

这会将所有内容放入缓冲区。

关于PHP 错误 "Can not modify header information",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17234366/

相关文章:

php - 在一个类中引用其他类 - php

javascript - iframe 随内容变化自动调整高度

javascript - 获取 ng-repeat 的匹配 ng-if 的数量

Github 页面、HTTP header

android - 如何获取默认内容类型 Android Volley

node.js - 如何在 Node.js 中使用相同的键设置多个 http header 字段?

php - 来自动态 JSON 的 Fullcalendar 加载事件

php - 如何在 PHP 中启用 imagecreatefromgif/imagecreatefromjpeg/imagecreatefrompng?

php - 第 463 行的 fatal error : Call to a member function getModelInstance() on a non-object in/Applications/XAMPP/xamppfiles/htdocs/magento/app/Mage. php

javascript - 如何使用 JS 获取 "active page"状态类