简体   繁体   中英

Php 5.4 Cannot modify header information - headers already sent

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM