Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: headers already sent error [duplicate]

I have a CI app that has a auth controller and switchuser function. Basically all it does it take an ID from the URI, get some user data from the ID, assign some session data then load a view.

function switch_user(){
    if(!$this->auth_lib->is_admin()){
       $this->session->set_flashdata('status', 'You need to be an administrator to access this page.');
       redirect('auth/login');
    }

    if($id = $this->uri->segment(3)):
            $this->load->model('auth_model', 'auth');
            $username = $this->auth->get_username($id)->account_name;
            $this->session->set_userdata(array('at_id' => $id, 'username' => $username));
            $this->session->set_flashdata('status','User switched.');
    endif;
    $this->load->model('clients_model', 'clients');
    $data['users'] = $this->clients->get_at_clients();
    $this->load->view('auth/switch', $data);
}

I'm getting the following error:

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\cp\application\controllers\auth.php:130)

Line 130 is $username = $this->auth->get_username($id)->account_name;

Here is the model function:

function get_username($at_id){
     $portal = $this->load->database('warehouse', TRUE);
     $portal->select('account_name');
     $portal->where('account_id', $at_id);
     $portal->from('wh_account');

     $query = $portal->get()->result();

     return $query[0];
}

I don't understand what's happening. When I run the code locally I don't get the error. But when I run it remotely i.e over the internet I get that headers error.

Can anyone help identify the problem?

Thanks,

Billy

UPDATE

I had actually put a var_dump around the $username = $this->auth->get_username($id)->account_name; line which was causing the error. I don't know know why though :(

like image 699
iamjonesy Avatar asked Jan 21 '23 09:01

iamjonesy


1 Answers

I would check that you have no closing PHP tag in any of your models, controllers or libraries - this will often cause this type of error.

like image 158
BrynJ Avatar answered Jan 26 '23 00:01

BrynJ