CodeIgniter header already sent and ob_start() at the beginning of index.php

codeigniter, php

Solution

Output generated by the first warning will trigger the second warning (i.e: Cannot modify header information ). In theory, if you solve the first warning, the second should auto-fix.

Open `system/core/Input.php` and at line #351 replace:

$this->ip_address = $_SERVER['REMOTE_ADDR'];

with:

$this->ip_address = $this->server('remote_addr');

Problem

In my application, I can ensure that there is no such a “un-intentional” whitespace or whatever. I don’t perform “echo” at controller or model, but there is always “header already sent” error on the log file (Nothing shown in browser). I don't have any closing php tag too. The only "suspicious" thing is I load session and input library at both, the model and the controller. This is actually not a big problem, but the error become terrible if I use CLI request. After browsing a while, I find this answer at stack overflow: https://stackoverflow.com/a/13783368/755319 It said that I can add ob_start() for output buffering at the beginning of index.php ``` <?php ob_start(); /* *--------------------------------------------------------------- * APPLICATION ENVIRONMENT *--------------------------------------------------------------- ``` Is is save to do such a thing? EDIT: Okay, I might be wrong and miss something here. Is there any programmatic way to find what's wrong? (A program to find wrong php tag, whitespace or something)? EDIT AGAIN: The error is only generated in CLI mode. The error is as follow: ``` <h4>A PHP Error was encountered</h4> <p>Severity: Notice</p> <p>Message: Undefined index: REMOTE_ADDR</p> <p>Filename: core/Input.php</p> <p>Line Number: 351</p> </div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;"> <h4>A PHP Error was encountered</h4> <p>Severity: Warning</p> <p>Message: Cannot modify header information - headers already sent by (output started at /home/gofrendi/public_html/No-CMS/system/core/Exceptions.php:185)</p> <p>Filename: libraries/Session.php</p> <p>Line Number: 675</p> ```

Original source

Related problems