I am having difficulty setting up a admin section with a codeigniter.
I have just added a new folder in controller folder called fam_Admin.
So structure is like following:
-controllers
—>fam_admin
—->-login.php
in routes.php I added the following code:
$route['fam_admin'] = "fam_admin/login";
<?php
class Login extends CI_Controller {
public function index() {
//$login['main_content'] = '/fam_admin/login';
//$this -> load -> view('fam_admin/includes/template', $login);
$this -> load -> view('fam_admin/login');
}
public function validate_login() {
$username = $this -> input -> post('username');
$password = $this -> input -> post('password');
$this -> load -> library('form_validation');
$this -> form_validation -> set_rules('username', 'username', 'trim|required');
$this -> form_validation -> set_rules('password', 'password', 'trim|required|callback_credential_check');
if ($this -> form_validation -> run() == FALSE) {
$this -> load -> view('fam_admin/login');
}
}
public function credential_check() {
//Field validation succeeded. Validate against database
$username = $this -> input -> post('username');
$password = $this -> input -> post('password');
$this -> load -> model('fam_admin/admin');
//query the database
$query = $this -> admin -> check_login($username, $password);
if (!$query) {
$this -> form_validation -> set_message('login_check', 'Invalid username or password');
return false;
$this -> index();
} else {
$data = array(
'username' => $this -> input -> post('username'),
'is_logged_in' => true
);
$this -> session -> set_userdata($data);
//redirect('fam_admin/main_section');
redirect('site/logged_in');
}
}
}
?>
Above is the code for login.php but when the login credentials are validated it gives following error in google chrome. You can try it by entering false credentials and it should give you errors and load the same login form.
The website encountered an error while retrieving http://www.famtripsandinspectionvisits.com/index.php/fam_admin/login/validate_login. It may be down for maintenance or configured incorrectly.
For one thing, this is an issue:
Once you
returnsomething, that’s it – the rest of the code won’t run – so theindexmethod doesn’t get called and there is no output at all.…but, you shouldn’t be loading views in a function that’s supposed to just handle data validation.
I would rearrange things a bit, try simplifying your controller to this: