Magento Debug HEADERS ALREADY SENT错误

sul*_*man 13 debugging header magento

我在system.log文件中收到以下错误:

 2011-01-12T14:16:52+00:00 DEBUG (7): HEADERS ALREADY SENT: 
 [0] C:\xampp\htdocs\www.mysite.com\app\code\core\Mage\Core\Controller\Response\Http.php:44
 [1] C:\xampp\htdocs\www.mysite.com\lib\Zend\Controller\Response\Abstract.php:727
 [2] C:\xampp\htdocs\www.mysite.com\app\code\core\Mage\Core\Controller\Response\Http.php:75
 [3] C:\xampp\htdocs\www.mysite.com\app\code\core\Mage\Core\Controller\Varien\Front.php:188
 [4] C:\xampp\htdocs\www.mysite.com\app\code\core\Mage\Core\Model\App.php:304
 [5] C:\xampp\htdocs\www.mysite.com\app\Mage.php:596
 [6] C:\xampp\htdocs\www.mysite.com\index.php:81
Run Code Online (Sandbox Code Playgroud)

我知道"已发送的标头"是什么意思,但我不知道是什么文件导致了这个并且跟踪并没有真正给我任何信息.

有没有找到违规文件的方法?

谢谢!

Ala*_*orm 17

这是艰难的方式.

找到正在执行日志记录的文件中的位置

C:\xampp\htdocs\www.mysite.com\app\code\core\Mage\Core\Controller\Response\Http.php 
Mage::log('HEADERS ALREADY SENT: '.mageDebugBacktrace(true, true, true));
Run Code Online (Sandbox Code Playgroud)

添加日志记录以获取到目前为止所包含/所需的每个文件的副本

Mage::log(print_r(get_included_files(),true));
Run Code Online (Sandbox Code Playgroud)

您可以将此日志记录直接添加到核心文件中,如果您记得将文件恢复到预先混淆状态,或者您可以添加临时副本

app/code/local/Mage/Core/Controller/Response/Http.php
Run Code Online (Sandbox Code Playgroud)

只要你记得在完成后删除它(或者只是使用git).

检查此文件列表为平时的空白嫌疑人,然后检查其可能产生的任何输出功能(echo,print,readfile,有可能更)

  • 这就是我一直在寻找的谢谢。我将按照我的方式完成这个列表,并在几年后尝试在完成后向这里报告。谢谢! (2认同)

Ala*_*orm 16

这是一种更简单的方法.

查看canSendHeaders文件中的方法

lib/Zend/Controller/Response/Abstract.php
Run Code Online (Sandbox Code Playgroud)

添加一些日志记录

public function canSendHeaders($throw = false)
{
    $ok = headers_sent($file, $line);
    // to get PHP's report on which file is sending a header.
    if ($ok !== false){
        Mage::log('File: ' . $file, null, 'exception.log', true);
        Mage::log('Line: ' . $line, null, 'exception.log', true);
    }

    if ($ok && $throw && $this->headersSentThrowsException) {
        #require_once 'Zend/Controller/Response/Exception.php';
        throw new Zend_Controller_Response_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
    }

    return !$ok;
}
Run Code Online (Sandbox Code Playgroud)


小智 9

从Mage_Core_Controller_Response_Http - > sendHeaders()抛出该错误.此函数调用实际执行检查的超类函数,以查看是否已发送标头,Zend_Controller_Response_Abstract - > canSendHeaders().

Zend_Controller_Response_Abstract类处理(除其他事项外)发送响应头并跟踪上次发送头(以及从哪个文件和行).这是函数的样子,我们将在第316行到lib\Zend\Controller\Response\Abstract.php进行更改:

public function canSendHeaders($throw = false) {
    $ok = headers_sent($file, $line);
    if ($ok && $throw && $this->headersSentThrowsException) {
        #require_once 'Zend/Controller/Response/Exception.php';
        throw new Zend_Controller_Response_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
    }
    return !$ok;
}
Run Code Online (Sandbox Code Playgroud)

至:

public function canSendHeaders($throw = false)
{
    $ok = headers_sent($file, $line);

    if ($ok) {
        Mage::log('Cannot send headers; headers already sent in ' . $file . ', line ' . $line, null, 'headers.log');
    }

    return !$ok;

    #if ($ok && $throw && $this->headersSentThrowsException) {
    #    #require_once 'Zend/Controller/Response/Exception.php';
    #    throw new Zend_Controller_Response_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
    #}
    #return !$ok;
}
Run Code Online (Sandbox Code Playgroud)

这将在/var/log/header.log中记录错误.


xyp*_*oid 8

您在Magento中遇到的最常见的地方是直接从控制器输出内容.

而不是做

echo $string; 
Run Code Online (Sandbox Code Playgroud)

在控制器内,执行以下操作:

$this->getResponse()->setBody($string);
Run Code Online (Sandbox Code Playgroud)


clo*_*eek 5

我也看到了.我认为它与WYSIWYG中的图像有关.尝试通过管理员(特别是CMS页面)观察日志,您可能会看到它发生.这是无害的.