5

I have an annoying HEADER ALREADY SENT error in system.log.

It is not easy to debug it, what I could do was to improve the logging to isolate the source of the issue (has described here):

Cannot send headers; headers already sent in lib/Varien/Image/Adapter/Gd2.php, line 133

Line 133 of Gd2.php:

call_user_func($this->_getCallback('output'), $this->_imageHandler);

And it is part of the method display:

public function display()
    {
        header("Content-type: ".$this->getMimeType());
        call_user_func($this->_getCallback('output'), $this->_imageHandler);
    }

How to go on ?

5
  • 2
    On which pages / requests do you get the error? If you don't know, recording the backtrace (Varien_Debug::backtrace(), github.com/magento/bugathon_march_2013/blob/master/lib/Varien/…) may be useful. Commented Sep 4, 2013 at 8:22
  • Normally the error is something like: output started in <filename> on line 12. Commented Sep 4, 2013 at 8:44
  • @MatthiasZeis the error is happening here and there on live site ( but I cannot understand on witch request ) ... the point is how can I find out the request ? Can you give more info about this ?
    – Fra
    Commented Sep 4, 2013 at 10:02
  • I would examine PHP files to make sure you don't have something such as Zend_Debug::dump() (or other browser output, echo, etc) floating around in a controller that responds to AJAX requests.
    – pspahn
    Commented Sep 5, 2013 at 0:25
  • @Francesco Go to the method mentioned in your link and write data like the backtrace (see above) or your module/controller/action information (see blog.chapagain.com.np/…) to your file. Please also pay attention to what FabianBlechschmidt wrote. Commented Sep 6, 2013 at 7:38

3 Answers 3

6

This is a tough one. I had an errant whitespace character at the beginning of a core class definition which screwing with me for a couple months till I realized what was going on.

Someone will write an extension one day to fix this, but till they do, one way to troubleshoot in Magento would be to start output buffering manually in index.php, then try to catch the output culprit in Varien_Autoload:

index.php:

ob_start();

lib/Varien/Autoload.php: (see note below)

public function autoload($class)
{
    if (max(array_column(ob_get_status(true),'buffer_used'))) {
        header('Content-Type: text/plain');
        ob_end_clean();
        debug_print_backtrace(2);
        die;
    }
    //... rest of method as normal ...
}

If you for example place a space at the beginning of Mage/Page/Block/Html/Footer.php then you would see the following:

#0  Varien_Autoload->autoload()
#1  spl_autoload_call() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Page/Block/Html/Footer.php:43]
#2  Mage_Page_Block_Html_Footer->_construct() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/lib/Varien/Object.php:112]
#3  Varien_Object->__construct() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Model/Layout.php:491]
#4  Mage_Core_Model_Layout->_getBlockInstance() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Model/Layout.php:437]
#5  Mage_Core_Model_Layout->createBlock() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Model/Layout.php:472]
#6  Mage_Core_Model_Layout->addBlock() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Model/Layout.php:239]
#7  Mage_Core_Model_Layout->_generateBlock() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Model/Layout.php:205]
#8  Mage_Core_Model_Layout->generateBlocks() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Model/Layout.php:206]
#9  Mage_Core_Model_Layout->generateBlocks() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Controller/Varien/Action.php:344]
#10 Mage_Core_Controller_Varien_Action->generateLayoutBlocks() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Cms/Helper/Page.php:113]
#11 Mage_Cms_Helper_Page->_renderPage() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Cms/Helper/Page.php:52]
#12 Mage_Cms_Helper_Page->renderPage() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Cms/controllers/IndexController.php:45]
#13 Mage_Cms_IndexController->indexAction() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Controller/Varien/Action.php:419]
#14 Mage_Core_Controller_Varien_Action->dispatch() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:250]
#15 Mage_Core_Controller_Varien_Router_Standard->match() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Controller/Varien/Front.php:176]
#16 Mage_Core_Controller_Varien_Front->dispatch() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/code/core/Mage/Core/Model/App.php:354]
#17 Mage_Core_Model_App->run() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/app/Mage.php:683]
#18 Mage::run() called at [/Volumes/dev-cs/sites-enabled/optimizely/optimizely.dev/index.php:87]

This won't catch everything, so the offending file won't always be line #1, but it can get you very close to the offending context.

Note: I'm using array_column() here, a GREAT new function from Ben Ramsey in php 5.5. Without php 5.5 you can define it using his code, which is available on Github.

3
  • This looks a good solution. Right now I tried to add request $_SERVER['REQUEST_URI'] information to the log of the function canSendHeaders() ...
    – Fra
    Commented Sep 19, 2013 at 5:08
  • Not sure what good that will do. Nothing in your app should be triggering output aside from the response object singleton.
    – benmarks
    Commented Sep 19, 2013 at 11:50
  • using phpcs would also prevent this, as it gives you a warning for such "syntax errors"
    – Flyingmana
    Commented Feb 2, 2015 at 22:12
2

Apologies, I know this is not an answer but for some reason we can only comment if we have a reputation of 50. I could post another question the same but doubtless it will be closed as a duplicate! I want to comment on this question with some additional information that has not been mentioned here and that might lead to an answer.

I've also been investigating this issue thinking it might have something to do with admin users being logged out unexpectedly when working on pages and static blocks (another question). As part of this investigation I found that this exact log is generated whenever images are displayed in CMS pages or static blocks. It doesn't have to be on the page/block itself, simply open the Insert/Edit Image dialog and then browse for uploaded images - there will be one such a log for each image displayed. Same for images in pages/static blocks. Turn on/off the WYSIWYG editor and you'll get a log for each image on the page.

So to me this looks like a core Magento bug (I'm using 1.7.0.2). It's not surprising really given that displaying an image attempts to change the header (see code in the question).

Does anyone have a solution to this?

As it's only when editing pages or static blogs that admin gets logged out I'm guessing the two are somehow related. [This logout is not a session lifetime config issue - that works fine everywhere else on the dashboard - it's only pages/blocks that this happens and it can happen after just a few minutes when you go to save the changes you've been actively working on.]

4
  • This seems relevant to the original question. It should also be an acceptable new question of its own - would be good to back link to this question to note the difference.
    – benmarks
    Commented Feb 4, 2014 at 18:18
  • I suggest you to try to replicate this on a clean 1.7.0.2 with samples data so you can be sure it is a core bug and not something related to your installation ... I think you can open a separate question.
    – Fra
    Commented Feb 5, 2014 at 5:35
  • I've now raised this as another question. How to fix: HEADERS ALREADY SENT and GD2 Commented Feb 5, 2014 at 16:55
  • Oh, the logout was another issue. Commented Feb 5, 2014 at 17:27
0

Most likely that error is caused by an existing image that GD is trying to parse via imagepng/imagejpg/etc functions. If you use rsync/etc you may have corrupted image headers within wysiwyg directory.

Don't waste your time tracking down the headers.

Simply go in media/wysiwyg/ directory and check if you have existing content (run ls -alh to see hidden files). If you do, back it up and recreate it with proper server permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.