0

So basically, I'm getting an error message which reads:

Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\star\application\controllers\process_login.php:1)

I know what is the meaning of that error but I can't figure out where the output was started. I've no whitespaces in the process_login.php file nor anything echo-ed out as well.

I access the login form via the http://localhost/star/index.php/star URL

star Controller

class Star extends CI_Controller {
    
    public function index()
    {       
        $this->load->view('login');
    }
}

On form submit, I'm posting to the process_login Controller.

process_login Controller (it doesn't even have a closing tag to avoid whitespace)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Process_login extends CI_Controller {
    public function index()
    {       
        $this->load->library('form_validation');        
        $this->form_validation->set_rules('userid', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 
        'required|callback_check_valid['.trim($this->input->post('userid')).']');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('login');
        }
        else
        {                   
            redirect('dashboard'); // this is the problem area
        }
    }
    
    public function check_valid($pw, $un)
    {       
        if($un)
        {
            $this->load->model('user');
            if($this->user->is_authenticated($un, $pw))
            {               
                return true;
            }
            else
            {
                $this->form_validation->set_message('check_valid', 
                  'Invalid login. Please try again!');
                return false;
            }
        }       
    }
}

/* End of file process_login.php */

dashboard Controller

class Dashboard extends CI_Controller {
    
    public function index()
    {       
        $this->load->view('admin_area', array('page_title'=>'Dashboard');       
    }
}

I'm assuming the process_login.php:1 means the output started from Line 1 of that file? If so, I don't have any output or whitespace in that file. Then why is it that I'm getting the error?

Debugging

After removing everything from the process_login.php file, I'm still getting the same error. This is what the stripped down version of the file looks like:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Process_login extends CI_Controller { 
    public function index()
    {           
        redirect('dashboard');
    }
}

I'm starting to think the problem might be in some other file which are being loaded before this controller file. Hence, it's saying that the output started from Line 1.

Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119
  • please show us the whole process_login.php script... – goldlife Jan 13 '15 at 09:08
  • I'm sorry, I disagree there with @ShaifulIslam I don't think it's a duplicate. That's because I know why that error occurs. I just don't know why is it happening in my case – asprin Jan 13 '15 at 09:11
  • Try to debug it.Some step 1.remove full code and only keep die() see result.2.add more code step by step and try to find where the problem is found. I think you know `headers already sent error` so try to find it by debugging.You may not find it with your eye. – Shaiful Islam Jan 13 '15 at 09:17
  • If I remove everything from the `index()` function of the `process_login` controller and keep just the `redirect('dashboard')` line, I'm still getting the same error – asprin Jan 13 '15 at 09:22
  • I have tested your code and its working.I did not told you to only remove from index function.Remove everything from your that php file and only keep this ` – Shaiful Islam Jan 13 '15 at 09:28
  • is your file in some folder...??...if so then redirect('folder/dashboard'); – parth Jan 13 '15 at 11:49

3 Answers3

1

I managed to solve it.

I referred to this SO Answer and it worked. Not sure how can the main index.php be the trouble maker. Anyone care to explain it please?

Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119
0

i will say same as @Shaifullslam , look here at the poin 3. It could be you unintentionally hit enter while try look into the index.php. Cause i did the that mistake.

about the ob_start(); you can read here so basically why it's not give you error because the output of the respone is buffered first by php. so it's not causing error. While buffer is on, no output or respone will be sent by php. Take a easy read php manual and also there is question similar

Community
  • 1
  • 1
Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41
  • Nope. There isn't any whitespace (intentional or unintentional) in main `index.php` file – asprin Jan 13 '15 at 09:57
  • dude upload the 50 line index.php and full process_login.php as @goldlife ask – Adi Prasetyo Jan 13 '15 at 10:33
  • just place it on your question and make sure you show the right index.php (star/index.php) on the public folder not in the CI application, and try debug to give us more clue before you redirect it to Dashboard – Adi Prasetyo Jan 13 '15 at 10:47
0

Try this

redirect(base_url().'process_login/');
Jigar Patel
  • 197
  • 6