Magento 调试标头已发送错误

发布于 2024-10-12 02:42:10 字数 751 浏览 5 评论 0原文

我在我的 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

我知道“标头已发送”的含义,但我不知道是什么文件导致了此错误,并且跟踪并没有真正为我提供任何信息。

有没有办法找出有问题的文件?

谢谢!

I am receiving the following error in my system.log file:

 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

I know what "headers already sent" means but I have no idea what file is causing this and the trace doesn't really give me any information.

Is there a way of finding out the offending file?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

放手` 2024-10-19 02:42:10

这是困难的方法。

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

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

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

Mage::log(print_r(get_included_files(),true));

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

app/code/local/Mage/Core/Controller/Response/Http.php

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

检查此文件列表中是否有常见的空格嫌疑,然后检查它们是否有可能产生输出的任何函数(echoprintreadfile,可能还有更多)

Here's the hard way.

Find the location in the file that's doing the logging

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

Add logging to get a copy of every file included/required so far

Mage::log(print_r(get_included_files(),true));

You can add this logging directly to the core file if you remember to restore the file to it's pre messed with condition, or you can add a temporary copy at

app/code/local/Mage/Core/Controller/Response/Http.php

as long as you remember to remove it when you're done (or just use git).

Check this list of files for the usual white-space suspects, and then check them for any functions that might produce output (echo, print, readfile, there's probably more)

韶华倾负 2024-10-19 02:42:10

这是一个更简单的方法。

查看文件中的 canSendHeaders 方法

lib/Zend/Controller/Response/Abstract.php

添加一些日志记录

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;
}

Here's an easier way.

Look at the canSendHeaders method in file

lib/Zend/Controller/Response/Abstract.php

Add some logging to

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;
}
如此安好 2024-10-19 02:42:10

该错误是从 Mage_Core_Controller_Response_Http -> 引发的发送标头()。该函数调用超类函数,该函数实际上执行检查以查看标头是否已发送,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;
}

收件人:

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;
}

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

That error is thrown from Mage_Core_Controller_Response_Http -> sendHeaders(). This function calls the super class function that actually does the check to see whether or not headers have already been sent, Zend_Controller_Response_Abstract -> canSendHeaders().

The Zend_Controller_Response_Abstract class handles, among other things, sending response headers and tracking the last time the headers were sent (and from what file and line). Here is what that function looks like, and where we'll make a change around line 316 to 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;
}

To:

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;
}

This will log the error in /var/log/header.log.

百变从容 2024-10-19 02:42:10

在 Magento 中最常见的情况是直接从控制器输出内容。

不要

echo $string; 

在控制器中执行此操作,而是执行以下操作:

$this->getResponse()->setBody($string);

The most common place you run into this in Magento is when you output content directly from the controller.

Instead of doing

echo $string; 

within a controller, do this:

$this->getResponse()->setBody($string);
蝶…霜飞 2024-10-19 02:42:10

我也看到这个。我认为这与所见即所得的图像有关。尝试在浏览管理(尤其是 CMS 页面)时查看日志,您可能会看到这种情况发生。这是无害的。

I see this too. I think it has something to do with images in WYSIWYG. Try watching the logs whilst going through the admin (especially CMS pages) and you might see it happen. It's harmless.

ゝ杯具 2024-10-19 02:42:10

也许这对某人有帮助:
当我在 CMS 中编辑页面时打开 Magento 的 WYSIWYG 时,我收到类似的消息 ->页面(默认情况下我禁用了所见即所得,因此我必须单击“显示/隐藏编辑器”才能启用它)。如果页面包含 CMS 标签,例如:

{{store url='my-other-page'}}

单击“显示/隐藏编辑器”后,此消息会出现在 system.log 中:

2013-04-06T11:10:38+00:00 DEBUG (7): HEADERS ALREADY SENT: <pre>[0] ...\app\code\core\Mage\Core\Controller\Response\Http.php:52
[1] ...\lib\Zend\Controller\Response\Abstract.php:766
[2] ...\app\code\core\Mage\Core\Controller\Response\Http.php:83
[3] ...\app\code\core\Mage\Core\Controller\Varien\Front.php:188
[4] ...\app\code\core\Mage\Core\Model\App.php:354
[5] ...\app\Mage.php:683
[6] ...\index.php:87
</pre>

Maybe it will be helpful for someone:
I get similar message when I turn on Magento's WYSIWYG when editing pages in CMS -> Pages (I've got WYSIWYG disabled by default so I have to click "Show/Hide Editor" in order to enable it). If page contains CMS tags such as for example:

{{store url='my-other-page'}}

this message apears in the system.log after I click "Show/Hide Editor":

2013-04-06T11:10:38+00:00 DEBUG (7): HEADERS ALREADY SENT: <pre>[0] ...\app\code\core\Mage\Core\Controller\Response\Http.php:52
[1] ...\lib\Zend\Controller\Response\Abstract.php:766
[2] ...\app\code\core\Mage\Core\Controller\Response\Http.php:83
[3] ...\app\code\core\Mage\Core\Controller\Varien\Front.php:188
[4] ...\app\code\core\Mage\Core\Model\App.php:354
[5] ...\app\Mage.php:683
[6] ...\index.php:87
</pre>
脸赞 2024-10-19 02:42:10

当我以一种“hacky”方式构建我的 Ajax 请求时,我收到了这个错误,直接回显内容而不是通过布局发送它们。一旦我通过布局将它们发送出去,错误就消失了。在这里查看我自己的问题:

最佳输出方式来自 Magento 管理扩展的 ajax 数据

向 @alan 提供他对我的问题的回答。

I received this error when I built my Ajax request in a "hacky" way, echoing the contents directly instead of sending them out through the layout. Once I sent them out through the layout, the errors disappeared. See my own question here:

Best way to output ajax data from a Magento Admin Extension

props to @alan for his answer to my question.

还如梦归 2024-10-19 02:42:10

我认为这可能是 OnePageCheckout 扩展问题。我在 Magento 中也有同样的错误,而且似乎这个错误并不那么流行。我还安装了 OnePageCheckout。但是,当然,这可能只是巧合。

I think this might be OnePageCheckout extension problem. I have the same error in Magento, and seems that this error is not so popular. Also I have OnePageCheckout installed. But, of course, this might be just coincidence.

昵称有卵用 2024-10-19 02:42:10

进行 Ajax 调用时出现相同问题

当我进行 Ajax 调用并直接从控制器调用模板时,我收到了日志。

当我更改代码并在布局 xml 文件中创建块时。日志错误已修复。

Same Issue while making Ajax Call

I was getting the Log when i was making Ajax call and calling the template directly from controller.

When i changed the code and created block in layout xml file. the log error got fixed.

一曲爱恨情仇 2024-10-19 02:42:10

我在安装 Magento 时遇到同样的问题。

就我而言,在 PHP 中启用 output_buffering 解决了这个问题。

在 PHP 5.6 的 xampp 中,默认启用 output_buffering

在 PHP 5.3 的 xampp 中,output_buffering 默认情况下处于禁用状态。

I have the same problem while installing Magento.

In my case enabling output_buffering in PHP solved the issue.

In xampp with PHP 5.6 output_buffering is enabled by default.

In xampp with PHP 5.3 output_buffering is disabled by default.

我早已燃尽 2024-10-19 02:42:10

在我们的例子中,这是 Magento CE 1.9.2.4 中的一个错误,该错误已在 Magento CE 1.9.3 中修复,在使用 WYSIWYG 处理图像时发生。我们刚刚做了一个小扩展,它覆盖了 \app\code\core\Mage\Adminhtml\controllers\Cms\WysiwygController.php 中的函数directiveAction()。有关更多详细信息,请参阅(德语)这里

In our case this was a bug in Magento CE 1.9.2.4, which was fixed in Magento CE 1.9.3, happened when using WYSIWYG with images. We just made a little extension which overwrites the function directiveAction() in \app\code\core\Mage\Adminhtml\controllers\Cms\WysiwygController.php. For more Details see (in German) here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文