3

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>
Community
  • 1
  • 1
goFrendiAsgard
  • 4,016
  • 8
  • 38
  • 64
  • Don't use `ob_start()` it's like putting a band-aid on a gunshot. Try to fix the problem code. – kittycat Mar 07 '13 at 08:33
  • 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)? – goFrendiAsgard Mar 07 '13 at 08:36
  • Post the error that is being generated. – kittycat Mar 07 '13 at 08:47
  • Hi, glad to see you want to help that far. See my edited question. The error is only appeared in CLI mode. – goFrendiAsgard Mar 07 '13 at 09:11
  • According to the error message you added if you check line number 185 of this file `/home/gofrendi/public_html/No-CMS/system/core/Exceptions.php` you may find the culprit character! – Dale Mar 07 '13 at 09:27
  • Or.. On line number 351 of core/Input.php it is searching for `$_SERVER['REMOTE_ADDR']` which isn't being set, possibly because cli mode doesn't have that index set. This is throwing the exception and sending the headers shown in the later error. – Dale Mar 07 '13 at 09:30
  • @goFrendiAsgard you don't really have any whitespace. What is happening is you have an undefined index error. The error being output is what is causing the headers already sent as it is output just like a space would be. Fix that error and the headers sent error will be fixed too. I would open up a bug report [here](https://github.com/EllisLab/CodeIgniter/issues) as CI should be checking for the existence of the var before using it which it is not. – kittycat Mar 07 '13 at 09:32
  • Okay, thanks guys. I've fix the problem by "stupidly" put this: if(!isset($_SERVER['REMOTE_ADDR'])) $_SERVER['REMOTE_ADDR'] = '0.0.0.0'; on index.php – goFrendiAsgard Mar 07 '13 at 09:59

2 Answers2

8

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');
Alexandru Guzinschi
  • 5,675
  • 1
  • 29
  • 40
0

The general reason to avoid output buffering is that it buffers the output. This seems stupidly obvious, but if your output is being saved in a buffer until the end of the thing, then it's not being delivered to the browser until the end of the process, all at once. Meaning that your page doesn't appear to load gradually anymore, but the whole thing has to run first, making it seem slower.

So usually, when there's no other way to do it except output buffering, then you probably have a good argument for doing the same.