Magento Debug HEADERS ALREADY SENT error

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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!

回答1:

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)



回答2:

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


回答3:

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


回答4:

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.

http://codegento.com/2011/03/debugging-headers-already-sent-error/



回答5:

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.



回答6:

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: 
[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 


回答7:

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.



回答8:

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.



回答9:

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.



回答10:

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.



回答11:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!