4

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

iamjonesy
  • 24,732
  • 40
  • 139
  • 206

2 Answers2

8

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.

BrynJ
  • 8,322
  • 14
  • 65
  • 89
  • Thanks for your sugestion, just checked and all the closing tags are there :( – iamjonesy Feb 27 '11 at 13:11
  • @iamjonesy - that's my point there's *shouldn't* be any closing tags (this can cause whitespace to be outputted, which will trigger the headers). – BrynJ Feb 27 '11 at 17:19
  • Hi BrynJ apologies I actually added incorrect code. I had a var_dump wrapped around this line $username = $this->auth->get_username($id)->account_name; and once I removed it the headers error stopped. I'd removed it from my code paste because I thought it wouldn't have anything to do with the error. Why would a var_dump cause that error message? – iamjonesy Feb 28 '11 at 12:17
  • That kind of error is shown when you output / print data prematurely. You can fix it by putting a "die;" after the var_dump. – stef Feb 28 '11 at 14:16
1
  //Your Code is for redirect
    redirect('site/function1');

    //Alternate Code for solve this issue
    $url = 'site/function1';
    echo'
    <script>
    window.location.href = "'.base_url().'index.php?/'.$url.'";
    </script>
    ';

Why I'm getting "cannot modify header information headers already sent by registration_model" error in codeigniter?

Community
  • 1
  • 1
user572575
  • 1,009
  • 3
  • 25
  • 45
  • I used the second alternative way i.e. javascript and it worked well! more than anything else i've tried! thanks – sys_debug Nov 02 '12 at 08:40