• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A quick fix for session expiry on browser close in CodeIgniter 1.7.2

#1
[eluser]Unknown[/eluser]
Though the CodeIgniter 2.1.0 has included a new feature in Session class to define the session expiry on browser with the help of a configuration in the config.php

Code:
$config['sess_expire_on_close'] = TRUE;

it is not available there in CodeIgniter 1.7.2 and for some reason the session doesn't expire automatically on browser close in this version. Though the ideal way would of course be to upgrade the version to CodeIgniter 2.1.0 still for people like me for whom an immediate upgrade in their live projects is not possible the following could work as a quick fix.

1. Add a variable to the Session class in CodeIgniter\system\libraries\Session.php as below:

var $sess_expire_on_close = FALSE;

2. In that same Session class, in the function declared as _set_cookie($cookie_data = NULL) update the below mentioned lines

Code:
setcookie(
    $this->sess_cookie_name,
    $cookie_data,
    $expire,
    $this->cookie_path,
    $this->cookie_domain
  );
to
Code:
$expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
  
    setcookie(
      $this->sess_cookie_name,
      $cookie_data,
      $expire,
      $this->cookie_path,
      $this->cookie_domain
    );
3. Again in the Session class in the constructor CI_Session change

Code:
foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name',   'sess_expiration', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key)
  {
   $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
  }
to
Code:
foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key' , 'sess_expire_on_close' ) as $key)
  {
   $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
  }

4. Now in Config.php in add the following line
Code:
$config['sess_expire_on_close'] = TRUE;

These three steps should do the trick in CodeIgniter 1.7.2 and the sessions should be expiring on browser close automatically by now.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.