title: Codeigniter: headers already sent error
body: <p>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.</p>
  
  <pre><code>function switch_user(){
      if(!$this-&gt;auth_lib-&gt;is_admin()){
         $this-&gt;session-&gt;set_flashdata('status', 'You need to be an administrator to access this page.');
         redirect('auth/login');
      }
  
      if($id = $this-&gt;uri-&gt;segment(3)):
              $this-&gt;load-&gt;model('auth_model', 'auth');
              $username = $this-&gt;auth-&gt;get_username($id)-&gt;account_name;
              $this-&gt;session-&gt;set_userdata(array('at_id' =&gt; $id, 'username' =&gt; $username));
              $this-&gt;session-&gt;set_flashdata('status','User switched.');
      endif;
      $this-&gt;load-&gt;model('clients_model', 'clients');
      $data['users'] = $this-&gt;clients-&gt;get_at_clients();
      $this-&gt;load-&gt;view('auth/switch', $data);
  }
  </code></pre>
  
  <p>I'm getting the following error: </p>
  
  <pre><code>Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\cp\application\controllers\auth.php:130)
  </code></pre>
  
  <p>Line 130 is $username = $this->auth->get_username($id)->account_name;</p>
  
  <p>Here is the model function:</p>
  
  <pre><code>function get_username($at_id){
       $portal = $this-&gt;load-&gt;database('warehouse', TRUE);
       $portal-&gt;select('account_name');
       $portal-&gt;where('account_id', $at_id);
       $portal-&gt;from('wh_account');
  
       $query = $portal-&gt;get()-&gt;result();
  
       return $query[0];
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Can anyone help identify the problem?</p>
  
  <p>Thanks,</p>
  
  <p>Billy</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>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 :(</p>