Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Debug HEADERS ALREADY SENT error

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!

like image 213
sulman Avatar asked Jan 12 '11 14:01

sulman


4 Answers

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);
like image 63
xyphoid Avatar answered Nov 13 '22 10:11

xyphoid


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.

like image 29
egeelabs Avatar answered Nov 13 '22 08:11

egeelabs


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;
}
like image 17
Alan Storm Avatar answered Nov 13 '22 08:11

Alan Storm


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.

like image 5
clockworkgeek Avatar answered Nov 13 '22 10:11

clockworkgeek