0

Recently I upgraded to Php 5.4 as mentioned in this link on Ubuntu 12.04 LTS:-

  1. sudo add-apt-repository ppa:ondrej/php5-oldstable
  2. sudo apt-get update
  3. sudo apt-get dist-upgrade

Nothing is changed in my code. But now I am getting the below error:-

E_WARNING Cannot modify header information - headers already sent by (output started at Abstract.php:588) in functions.php on line 55

The relevant code that is throwing the error:-

 //Abstract.php
 public function outputBody()
    {
        $body = implode('', $this->_body);
        echo $body; //Line 588
    }
...

// functions.php 
 if ($exit) {
    if (strtolower(PHP_SAPI) != 'cli') {            
       header("HTTP/1.1 500 Internal Server Error"); //Line 55
    }
    echo Bob_Exception_Helper::printException($e, $logStr) . PHP_EOL;
    exit (1);
 }

I have added the below lines in both /etc/php5/cli/php.ini and /etc/php5/apache2/php.ini as mentioned in this question

output_buffering = On

Also everything was working fine on php 5.3. No code change was done after the upgrade. Just after upgrade to Php 5.4 this issue came up.

php --version
PHP 5.4.45-4+deprecated+dontuse+deb.sury.org~precise+1 (cli) (built: Jun 24 2016 08:30:18) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

The php code that is throwing the error:-

public function outputBody()
 {
        $body = implode('', $this->_body);
        echo $body; //As per the warning this line is throwing the error
 }

But still getting the same error? Can some one let me know how can I resolve this?

Community
  • 1
  • 1
tuk
  • 5,941
  • 14
  • 79
  • 162
  • 2
    This may help you - [How to fix “Headers already sent” error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987) – Tom Jan 25 '17 at 11:25
  • Yeah I have gone through that question. Updated my question. – tuk Jan 25 '17 at 11:33
  • There will be some PHP code producing this error - normally its referenced in the `E_Warning` - is there more to that than you have posted? – Tom Jan 25 '17 at 11:34
  • Updated the question. – tuk Jan 25 '17 at 12:10

2 Answers2

1

Ok, so your outputBody() method is being called prior to the code in your functions.php file.

It is this if statement triggering the error;

if (strtolower(PHP_SAPI) != 'cli') {            
    header("HTTP/1.1 500 Internal Server Error"); //Line 55
}

The PHP_SAPI constant has obviously changed values when you've upgraded PHP. This is triggering the header() method, but becuase outputBody() in Abstract.php has already ran and echo'd content to the page the headers have been set. Due to it not being possible to reset headers once they've sent, your header() method fails.

Have a look at why PHP_SAPI has changed and what it's changed to. Fixing this should solve your issue.

Tom
  • 4,257
  • 6
  • 33
  • 49
0

You can not modify header information, when there was already an output happening. (for example echo "xyz"; or just a space in front of your <?php tag). If you show the code to us, that producdes the error, we maybe can tell you the exact place, where it happens.