Wednesday, 16 May 2018

php - redirect() is giving header() error even though nothing is being output to the browser




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)




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:





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.


Answer



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?


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...