CodeIgniter Forums
Cannot modify header information - headers already sent by (output started at C:wampwwwappcoreMY_Controller.php:1) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Cannot modify header information - headers already sent by (output started at C:wampwwwappcoreMY_Controller.php:1) (/showthread.php?tid=57940)



Cannot modify header information - headers already sent by (output started at C:wampwwwappcoreMY_Controller.php:1) - El Forum - 04-28-2013

[eluser]cobolCowboy[/eluser]
Hello,

Thanks to this framework I've been able to learn some PHP and OOP after three decades of linear COBOL programming. Thanks for that.

I looked for an answer to this but to no avail.
I'm running a WAMP server, using PHP 5.3.10 and CI 2.1.3

MY_Controller, in the error text, which lives in app\core\MY_Controller.php looks like this...
Code:
<?php

class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        if (!$this->session->userdata('loggedin'))
        {
            redirect('login');
        }
    }
}

I did this so that any page that requires being logged in will extend this controller instead of CI_Controller,
but the weirdest part is that I have not yet extended this controller anywhere ?!

The full text of the error is this...
Quote:A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\wamp\www\app\core\MY_Controller.php:1)

Filename: libraries/Session.php

Line Number: 676


Considering that I have not overridden the session library, then I'm lead to believe that this must be pointing to sys\libraries\session.php.

Line 676 points directly to the setcookie statement...
Code:
setcookie(
     $this->sess_cookie_name,
     $cookie_data,
     $expire,
     $this->cookie_path,
     $this->cookie_domain,
     $this->cookie_secure
    );
Doesn't setting the cookie constitute output to the browser?
And once you do that, you can't then later send out browser content without getting this "severe" type error.
I commented this statement and the error went away. But then a cookie doesn't get set.
What's the ramifications of that? How do we solve this problem?


Cannot modify header information - headers already sent by (output started at C:wampwwwappcoreMY_Controller.php:1) - El Forum - 04-28-2013

[eluser]Aeisor[/eluser]
Have you saved this file (MY_Controller.php) with an invalid UTF-8 BOM, or with a new line before the "<?php"?

Try saving it as DOS encoding.