1

I am trying to use session in all controllers, but can't get success.

Here is my controller code of library.

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

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

    }

    public function is_logged_in()
    {
        $user = $this->session->userdata('username');
        return isset($user);
    }
}

?>

And here I am inherit It in my other controller file.

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

    public function CheckSession()
    {
        if ($this->is_logged_in())
        {
            echo '111';
        }
    }
}

I also try helper but giving me same error.

<?php
        function is_logged_in() {
        // Get current CodeIgniter instance
        $CI =& get_instance();
        // We need to use $CI->session instead of $this->session
        $user = $CI->session->userdata('user_data');
        if (!isset($user)) { return false; } else { return true; }
    }
    ?>

autoload.php part

$autoload['helper'] = array('url','form','file','login');

$autoload['drivers'] = array('session');

$autoload['libraries'] = array('database','session', 'email', 'form_validation', 'MY_Controller'); I am Following this link.

Community
  • 1
  • 1
Mahi
  • 180
  • 3
  • 15

1 Answers1

1

either you need to autoload session library in your authoload.php like this

$autoload['libraries'] = array('session');

or load session library in constructor of library file like this

public function __construct()
{
    parent::__construct();
    $this->load->library('session');
}
Sachin Kumar
  • 3,001
  • 1
  • 22
  • 47