I have a function index() in my Admin.php controller and I'm trying to execute the 'if' statement but it is executing the 'else' statement. The admin user and password that I am logging in is correct. There's something wrong in my code and could anyone here please help me. Thanks in advance. :)
Here's my code:
//Admin.php controller
<?php
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
if ($this->session->userdata('logged_in') !== TRUE) {
redirect('Login');
}
}
function index()
{
if ($this->session->userdata('level') === '1') {
$this->load->view('admin_view');
} else {
echo "Access Denied";
}
}
}
//Login.php controller
<?php
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Login_model');
}
public function index()
{
$this->load->view('login_view');
}
public function auth()
{
$username = $this->input->post('user_name', TRUE);
$password = $this->input->post('user_pass', TRUE);
$result = $this->Login_model->check_user($username, $password);
if ($result->num_rows() > 0) {
$data = $result->row_array();
$name = $data['user_name'];
$level = $data['user_lvl'];
$sesdata = array(
'user_name' => $username,
'user_lvl' => $level,
'logged_in' => TRUE
);
$this->session->set_userdata($sesdata);
if ($level === '1') {
redirect('Admin');
} elseif ($level === '2') {
redirect('User');
}
} else {
echo "<script>alert('Access Denied');history.go(-1);</script>";
}
$this->load->view('login_view');
}
}