如何修复PHP中的“Headers already sent”错误

时间:2014-02-18 07:12:08

标签: php

运行我的脚本时,我收到了以下几个错误:

Warning: Cannot modify header information 
- headers already sent by (output started at /some/file.php:12) 
in /some/file.php on line 23

错误消息中提到的行包含header()setcookie()次调用。

这可能是什么原因?以及如何解决它?

1 个答案:

答案 0 :(得分:1)

在您的页面中写入后使用标题会导致此类问题。我假设您的代码可能看起来像::

 <html>
   <head>
     <title> title </title>
     .........
   </head>

   <body>
    .....
     <?php 
        header() // checking some header
     ?>
     .....
   </body>
 </html>

如果是这样,请在html之前使用 header()或任何类型的回显或编写代码。但这不是正确的解决方案。您可以使用ob_start。这是最好的解决方案。检查this