moses toh moses toh - 4 months ago 119x
MySQL Question

Why call session not working? (codeigniter 3)

my config is like this :

$config['sess_driver'] = 'database'; // select database driver
$config['sess_save_path'] = 'ci_sessions'; // name of the mysql table
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 300;

My autoload is like this :

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

In controller login.php :

$this->session->set_userdata('club', 'chelsea');

In controller dashboard.php :

echo $this->session->userdata('club');

It did not succeed in calling the session club if different controller

But, when I call session club in the same controller(controller login.php), it's working

I try
in the constructor of
. But it's not success

Any solution to solve my problem?

Thank you


What the answer from @Ali suggests will work or load the session library in the constructor of Dashboard.php like so...

class Dashboard extends CI_Controller
  public function __construct()

  //the rest of controller's code

Or if you only need sessions in one particular function inside the controller load the library in that function instead.

The point is that the library has to be loaded somewhere before you can make calls to its functions.

The choice of autoloading or explicitly loading session in a controller might depend on how often your site needs to use sessions. If every controller uses them then chose autoload. If sessions are needed in only a few places then it is a waste of resources to load them where they are not used.