13

I have a problem with model in my codeigniter app. I'm getting this error of sending headers information. Basically, codeigniter is complaining about my registration model sending header information before anything else. How is it possible? I thought that models are only for holding db queries methods and nothing more. Can someone please explain me that?

This is how the beginning of a controller looks like:

function User()
{
    parent::Controller();
    $this->view_data['base_url'] = base_url();
    $this->load->model('User_registration_model');       // don't forget capital, it's important
    $this->load->model('user_map_model');                // don't forget capital, it's important
    $this->load->model('Tribe_model');                   // don't forget capital, it's important
    $this->load->library('email');                       // Loading email library
    $this->load->library('session');                     // sets up the session
    $this->load->library ('form_validation');            // Loading form validation library
    $this->load->helper(array('form', 'url'));  
}
4
  • In which file are you using the header functions, controller or view?
    – Sarfraz
    Commented Aug 3, 2010 at 9:00
  • I'm having all header information in the view Sarfaz. I should have mentioned that I needed to rewrite my registration_model.php because I've lost it when I tried to upload it to the server (I lost connection at the time) and didn't make any backup (I know a terrible sin programmers should never commit). But at least I know that there's something wrong with my model file.
    – Pavel
    Commented Aug 3, 2010 at 9:05
  • did you check for whitespace before/after php-tags? Commented Aug 3, 2010 at 9:12
  • Please quote the exact error message you are getting.
    – Pekka
    Commented Aug 3, 2010 at 9:16

3 Answers 3

40

place this ob_start(); on first line of index.php under your application directory like this :

<?php
ob_start();
/*
 *---------------------------------------------------------------
 * APPLICATION ENVIRONMENT
 *---------------------------------------------------------------
1
  • According to @Sarfraz answer, ob_start(); will turn output buffering on. While output buffering is active no output is sent from the script (other than headers).
    – Gery
    Commented May 16, 2016 at 13:02
34

Note that headers should be sent before anything else. Make sure that there is no code/html or even space/indentation before the header function and there is nothing before the first opening php tag <?php as well as ending tag ?> in your view.

0
6

Actually Some of Host Provider they are use PHP header(); to redirect our Site, So in that kind of servers if we use PHP header() it will give the error. I think so. In Code Igniter redirect(); is using PHP header() to redirect Our URL. So thats why it will give this Error!

So the only solution is use JavaScript to Solve this Issue, I am Using it! Its working well.

//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>
';

I don't know is this the right solution for the above issue, But I am using it, Its working 100%. Thanking you!

Not the answer you're looking for? Browse other questions tagged or ask your own question.