CodeIgniter Forums
Cannot modify header information - headers already sent - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Cannot modify header information - headers already sent (/showthread.php?tid=69464)



Cannot modify header information - headers already sent - dunkindonato - 11-27-2017

Hi! I'm using CodeIgniter for quite awhile now..and I'm currently facing an error



Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /project/system/core/Exceptions.php:271)

Filename: helpers/url_helper.php

Line Number: 564

Backtrace:

File: /project/application/core/MY_Controller.php
Line: 75
Function: redirect

File: /project/application/controllers/User.php
Line: 116
Function: user_view



This is Line 75 of My_Controller

PHP Code:
if( ! $this->session->email || $this->session->email == NULL || $this->session->isLoggedIn !== TRUE) {
     $this->session->sess_destroy();
     redirect(site_url('login'));
}
  

just incase this is needed, I used echo in my User controller because this is where I get some ajax request.

Thank you very much Smile


RE: Cannot modify header information - headers already sent - dunkindonato - 11-27-2017

UPDATE:

I just found out that this one might be the one causing problem.
PHP Code:
$data['pending_count']   = ($pending->count $pending->pages);
$data['paid_count'     = ($paid->count $paid->pages);
$data['cancelled_count'] = ($cancelled->count $cancelled->pages); 

I cannot use redirect(site_url('login')); below this code. But if I put redirect before $data[] it works just fine.

Is there any way I can solve my error? Thank you! Smile


RE: Cannot modify header information - headers already sent - InsiteFX - 11-28-2017

You get the headers already sent error because you have written to the output already.

Which is most Lilly what the $data is doing. So yes you would need to redirect before
you write the $data to the view.