FateWell
FateWell

Reputation: 45

Kill Session on browser close Codeigniter 3

I would like to set the session in codeigniter version 3 to expire on browser close .

I have googled and got below solution to fix the issue. And updated below line in application config

$config['sess_expiration'] = 0;
$config['sess_expire_on_close'] = TRUE;

This solution works perfectly.

But the issue is now browser session remains live until the browser is closed which will be a treat to my application.

Hence I wanted to keep 10 mins of session expire time also.

So seeking help from the forum to implement both scenarios in my application.

Thanks in advance.

Upvotes: 2

Views: 2828

Answers (2)

Jaydeep Gondaliya
Jaydeep Gondaliya

Reputation: 321

application\config\config.php

/*
|--------------------------------------------------------------------------
| 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']      = 600;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'dbapp_ci_sessions';
$config['sess_match_ip']        = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

/*

just set $config['sess_expiration']= 7200;to $config['sess_expiration'] = 600; and this will set your session to expire every 10 minutes. Also if you set $config['sess_expire_on_close'] = TRUE; as pointed, every time when browser is closed session will be killed.

Upvotes: 0

Noman
Noman

Reputation: 1487

I think you use this code, Here you go:

var expireSessionVar = function(e){
  //HERE YOUR CODE WHATEVER YOU WANT
};
window.unload = expireSessionVar;

Hope it will help you.

Upvotes: 1

Related Questions