codeigniterauthenticationcodeigniter-hmvc

Error Login In My HMVC Code Igniter


i have a problem with my login in application . in my login authentication a create a library name is "auth" and i also was call "auth" in autoload but when i call a function in "auth" the error say

Message: Undefined property: Auth::$session

here is my library "auth"

class Auth {
    public function cek_auth()
    {
        $this->ci =& get_instance();
        $this->sesi  = $this->ci->session->userdata('isLogin');
        $this->hak = $this->ci->session->userdata('stat');
        if($this->sesi != TRUE){
            $this->session->set_flashdata("pesan", "Sesi Login anda telah habis");
            redirect('user/login');
            exit();
        }

    }
    public function hak_akses($kecuali="")
    {
        if($this->hak==$kecuali){
            echo "<script>alert('Anda tidak berhak mengakses halaman ini!');</script>";
            redirect('dashboard');
        }elseif ($this->hak=="") {
            echo "<script>alert('Silahkan Login!');</script>";
            redirect('c_login','refresh');
        }else{

        }
    }
}

my controller : class Dashboard extends CI_Controller{

function __construct()
{
    parent::__construct();
    $this->auth->cek_auth(); //ngambil auth dari library
}
function index()
{
    $hak_akses = $this->session->userdata('lvl');
    if($hak_akses==1) {

        $data['content'] = 'dashboard';
        return $this->load->view('theme/index', $data);
    }

}

}

and my autoload :

$autoload['libraries'] = array('database','form_validation','session','auth','parser'); 

please give me a best solution , thanks


Solution

  • If you are using session in your code then make sure to autoload in config.php file by setting an encryption key. If you don't autoload it then you can load it using $this->load->library('session'). But you must set encryption key in order to autoload or load session in controller functions.

    Reference : Undefined property: CI::$session.

    Hope this helps :)