Issue related to ajax requests causing session logout codeigniter

1.4k views Asked by At

In my current application, I am making a lot of ajax requests to the backend.

Codeigniter version being used is 2.1.4.

So after a time the session logs out, I think due to session changing and an ajax request still making request using the past token.

With codeigniiter version 2.2.1, I see a fix for "Fixed a bug in the Session Library where session ID regeneration occurred during AJAX requests.". Since I have a expiration time of 2 hours and I have a single js page application with no page reload, with version 2.2.1 do I need to make a ajax request to change the token in background every 2 hour while no other ajax requests are happening.

Thanks.

2

There are 2 answers

0
Praveen D On

In config.php check and update below lines for session settings.

/*
|--------------------------------------------------------------------------
| Session Variables
|--------------------------------------------------------------------------
|
| 'sess_cookie_name'        = the name you want for the cookie
| 'sess_expiration'         = the number of SECONDS you want the session to last.
|   by default sessions last 7200 seconds (two hours).  Set to zero for no expiration.
| 'sess_expire_on_close'    = Whether to cause the session to expire automatically
|   when the browser window is closed
| 'sess_encrypt_cookie'     = Whether to encrypt the cookie
| 'sess_use_database'       = Whether to save the session data to a database
| 'sess_table_name'         = The name of the session database table
| 'sess_match_ip'           = Whether to match the user's IP address when reading the session data
| 'sess_match_useragent'    = Whether to match the User Agent when reading the session data
| 'sess_time_to_update'     = how many seconds between CI refreshing Session Information
|
*/
$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']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;
0
Abdul Manan On

this is a common issue i faced the same problem . for this you have to create custom session class that extend session and then autoload the custom session .

class MY_Session extends CI_Session {

public function sess_update()
{
    $CI =& get_instance();

    if ( ! $CI->input->is_ajax_request())
    {
        parent::sess_update();
    }
  }
}

for more details check this