Magento – Cannot modify header information – headers already sent by

headermagento2magento2.2.7

I'm using Magento 2.2.7 CE with Porto theme and seem to be randomly finding the following error in the var/report folder:

{"0":"Warning: Cannot modify header information - headers already sent by (output started at \/home\/passionh\/public_html\/vendor\/zendframework\/zend-http\/src\/PhpEnvironment\/Response.php:114) in \/home\/passionh\/public_html\/vendor\/magento\/framework\/Stdlib\/Cookie\/PhpCookieManager.php on line 148","1":"#0 [internal function]: Magento\\Framework\\App\\ErrorHandler->handler(2, 'Cannot modify h...', '\/home\/passionh\/...', 148, Array)\n#1 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Stdlib\/Cookie\/PhpCookieManager.php(148): setcookie('store', 'gb', 1577536511, '\/', '', false, true)\n#2 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Stdlib\/Cookie\/PhpCookieManager.php(121): Magento\\Framework\\Stdlib\\Cookie\\PhpCookieManager->setCookie('store', 'gb', Array)\n#3 \/home\/passionh\/public_html\/vendor\/magento\/module-store\/Model\/StoreCookieManager.php(61): Magento\\Framework\\Stdlib\\Cookie\\PhpCookieManager->setPublicCookie('store', 'gb', Object(Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata))\n#4 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Store\\Model\\StoreCookieManager->setStoreCookie(Object(Magento\\Store\\Model\\Store\\Interceptor))\n#5 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Store\\Model\\StoreCookieManager\\Interceptor->___callParent('setStoreCookie', Array)\n#6 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Interception\/Interceptor.php(153): Magento\\Store\\Model\\StoreCookieManager\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Store\\Model\\Store\\Interceptor))\n#7 \/home\/passionh\/public_html\/generated\/code\/Magento\/Store\/Model\/StoreCookieManager\/Interceptor.php(26): Magento\\Store\\Model\\StoreCookieManager\\Interceptor->___callPlugins('setStoreCookie', Array, Array)\n#8 \/home\/passionh\/public_html\/app\/code\/Bss\/GeoIPAutoSwitchStore\/Observer\/Redirect.php(462): Magento\\Store\\Model\\StoreCookieManager\\Interceptor->setStoreCookie(Object(Magento\\Store\\Model\\Store\\Interceptor))\n#9 \/home\/passionh\/public_html\/app\/code\/Bss\/GeoIPAutoSwitchStore\/Observer\/Redirect.php(431): Bss\\GeoIPAutoSwitchStore\\Observer\\Redirect->makeRedirects(Object(Magento\\Store\\Model\\Store\\Interceptor), Object(Magento\\Framework\\Event\\Observer), 'store')\n#10 \/home\/passionh\/public_html\/app\/code\/Bss\/GeoIPAutoSwitchStore\/Observer\/Redirect.php(203): Bss\\GeoIPAutoSwitchStore\\Observer\\Redirect->redirectFunction('GB', Object(Magento\\Framework\\Event\\Observer))\n#11 \/home\/passionh\/public_html\/app\/code\/Bss\/GeoIPAutoSwitchStore\/Observer\/Redirect.php(168): Bss\\GeoIPAutoSwitchStore\\Observer\\Redirect->handleRedirects(NULL, 0, 0, 0, '0', '213.205.242.104', '0', Object(Magento\\Framework\\Event\\Observer))\n#12 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Event\/Invoker\/InvokerDefault.php(72): Bss\\GeoIPAutoSwitchStore\\Observer\\Redirect->execute(Object(Magento\\Framework\\Event\\Observer))\n#13 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Event\/Invoker\/InvokerDefault.php(60): Magento\\Framework\\Event\\Invoker\\InvokerDefault->_callObserverMethod(Object(Bss\\GeoIPAutoSwitchStore\\Observer\\Redirect), Object(Magento\\Framework\\Event\\Observer))\n#14 \/home\/passionh\/public_html\/vendor\/magento\/framework\/Event\/Manager.php(66): Magento\\Framework\\Event\\Invoker\\InvokerDefault->dispatch(Array, Object(Magento\\Framework\\Event\\Observer))\n#15 \/home\/passionh\/public_html\/vendor\/magento\/framework\/App\/Http.php(147): Magento\\Framework\\Event\\Manager->dispatch('controller_fron...', Array)\n#16 \/home\/passionh\/public_html\/vendor\/magento\/framework\/App\/Bootstrap.php(257): Magento\\Framework\\App\\Http->launch()\n#17 \/home\/passionh\/public_html\/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http\\Interceptor))\n#18 {main}","url":"\/","script_name":"\/index.php"}

I don't know where to look to sort this issue, if anyone could point me in the right direction that would be great!

Best Answer

my experience with this is: that can be hard to troubleshoot.

the reason for this bug in my experience may be:

  • setting a cookie before the magento dispatch event is triggered is quite difficult to find but would give this error; that would be after installing a new module that has observer and set the cookie indeed on a wrong event
  • that can be after inserting into a php file a character before the php opening tag <?php. git status might show you these weird php openings..
  • in fact, I have not had it with Magento but the closing php ?> tag can give the same problem I believe. It is Magento recommended practice not to close the php script file (see some comments)

As you can see, these pointers are quite broad and I can feel if you would want to give up on either of these ideas..

Now, in practical terms, just disable all your custom modules, or latest modules added first and flush the cache, clear cookies, cache storage.. and then once the error has gone, re-enable some modules.

Related Topic