CodeIgniter Session Expire in Config file

2019-06-11 06:06发布

问题:

I am working on CodeIgniter framework.

What I want, is to set the session expiring time of the site to 3 hours ie, (3600*3=10800 seconds).

But when I set the $config['sess_expiration'] = 10800; in config.php, the session is getting expired within around 30 minutes of idle and is redirected to login page.

Why is this happening?

These are some of my session settings in config

  $config['sess_cookie_name']     = 'ci_session';
   $config['sess_expiration']      = 10800;
      $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'] = TRUE;
  $config['sess_time_to_update']  = 300;

回答1:

Make sure you have the ci_sessions table in your database.

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(16) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

EDIT: also, make sure you are not using $_SESSION versus $this->session->set_userdata($array);

Link: http://codeigniter.com/user_guide/libraries/sessions.html



回答2:

Check the code like this:

if($login)
{
   $this->session->sess_expire_on_close = TRUE;
}
else
{
   $this->session->sess_expiration = 60*60*3;
   $this->session->sess_expire_on_close = FALSE;
}

  $this->session->set_userdata('your_item'); 


回答3:

Check your configuration of session.gc_lifetime in php.ini.

By default, this is 1440, or 24 minutes. This means that whenever PHP's garbage-collector runs (it does this with a configured probability on every pageload), any sessions older than 24 minutes will be cleared out via the gc() function in your CodeIgniter session driver.