Monday, 28 August 2017

php - Codeigniter: headers already sent error

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 :(

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...