codeigniter login redirect too many to home controller

Viewed 838

i have one problem with my login and the views, i want once the user is logged and exits the session then redirect me to my home , but the user slip the url to root project or login url then it should be stay on home ., my code looks like this

MY_Controller

protected function isLogged(){
        if (!$this->session->userdata('log'))
            redirect('login');
    }

login

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

class Login extends MY_Controller {
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $data['module'] = 'Login';
        $this->load->view('header',$data);
        $this->load->view('login');
    }

    public function getAccess(){
        $username = $this->input->post('username', TRUE);
        $password = $this->input->post('password', TRUE);
        $result = $this->user->login($username,$password);
        if (!empty($this->input->post('username')) && !empty($this->input->post('password'))) {
            if (!$result) {
                $this->json(array('error' => 'invalid username or password'));
            }else{
                $data_session = array(
                    'id' => $result['id'],
                    'first_name' => $result['first_name'],
                    'last_name' => $result['last_name'],
                    'type' => $result['profile_id'],
                    'logged_in' => TRUE 
                );
                $this->session->set_userdata('log',$data_session);
            }
        } else {
            $this->json(array('empty' => 'You did not fill out the required fields.'));
        }
    } 

    public function logout(){
        $this->session->sess_destroy();
        redirect('login','refresh');
    }
}

home controller

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

class Home extends MY_Controller {

    public function __construct(){
        parent::__construct();
        $this->isLogged();
    }

    public function index(){
        $data = $this->session->userdata('log');
        $data['module']  = "Home";
        $data['fields']  = $this->getModules();
        $this->load->view('header',$data);  
        $this->load->view('index');
        $this->load->view('home');
        $this->load->view('footer');
    }
}

/* End of file Welcome.php */
/* Location: ./application/controllers/Welcome.php */
2 Answers
Related