Codeigniter: headers already sent error

16,922

Solution 1

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.

Solution 2

  //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?

Share:
16,922
iamjonesy
Author by

iamjonesy

Lead dev at Appointedd. Based in Edinburgh, Scotland. A few of my sites: http://findr.fm http://defaqto.io http://masquerade.eu01.aws.af.cm

Updated on June 09, 2022

Comments

  • iamjonesy
    iamjonesy almost 2 years

    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
    iamjonesy about 13 years
    Thanks for your sugestion, just checked and all the closing tags are there :(
  • Danny
    Danny about 13 years
    @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).
  • iamjonesy
    iamjonesy about 13 years
    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?
  • stef
    stef about 13 years
    That kind of error is shown when you output / print data prematurely. You can fix it by putting a "die;" after the var_dump.
  • sys_debug
    sys_debug over 11 years
    I used the second alternative way i.e. javascript and it worked well! more than anything else i've tried! thanks
  • Kostas
    Kostas about 9 years
    really helpful! Thank you