Welcome Guest, Not a member yet? Register   Sign In
RICK: Any solution yet, to get session expired on browser close?
#3

[eluser]drewbee[/eluser]
Exactly. You will need to extend the session class. I think its kind of weird that it assumes we want it set for a set amount of time.

Personally, for me, I've changed around the session class so much that you wouldn't even recognize the original CI Library. Thanks for pointing this out, it will be something I will be doing really soon.

I didn't like the fact that it made the assumption, and it was removed as i_like_ponies pointed out above.

Alas, it is easiest just to make a config option and go with that, so we don't mess with the session class too much. I am setting a configuration option that when TRUE, will follow code igniters current setup. We want to set a non-persistant cookie if we want it to expire at browser close.

In your config file add a new configuration option:
Code:
$config['sess_persistant_cookie']     = TRUE;

Update the base constructor to load the configuration value and set a default.

In your extended session library variable initialization:
find:
Code:
var $sess_expiration = 7200;

Replace with:

Code:
var $sess_expiration = 7200;
    var $sess_persistant_cookie = TRUE;

In the constructor, find:

Code:
// Set all the session preferences, which can either be set
        // manually via the $params array above or via the config file
        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);
        }

Replace with:

Code:
// Set all the session preferences, which can either be set
        // manually via the $params array above or via the config file
        foreach (array (
                'sess_encrypt_cookie',
                'sess_use_database',
                'sess_table_name',
                'sess_expiration',
                'sess_persistant_cookie',
                '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);
        }

Now in the function _set_cookie
find:
Code:
function _set_cookie($cookie_data = NULL) {
        if (is_null($cookie_data)) {
            $cookie_data = $this->userdata;
        }

        // Serialize the userdata for the cookie
        $cookie_data = $this->_serialize($cookie_data);

        if ($this->sess_encrypt_cookie == TRUE) {
            $cookie_data = $this->CI->encrypt->encode($cookie_data);
        } else {
            // if encryption is not used, we provide an md5 hash to prevent userside tampering
            $cookie_data = $cookie_data . md5($cookie_data . $this->encryption_key);
        }
        
        // Session or persistant cookie...
        
        // Set the cookie
        setcookie($this->sess_cookie_name, $cookie_data, $this->sess_expiration + time(), $this->cookie_path, $this->cookie_domain, 0);
    }

Replace with:

Code:
function _set_cookie($cookie_data = NULL) {
        if (is_null($cookie_data)) {
            $cookie_data = $this->userdata;
        }

        // Serialize the userdata for the cookie
        $cookie_data = $this->_serialize($cookie_data);

        if ($this->sess_encrypt_cookie == TRUE) {
            $cookie_data = $this->CI->encrypt->encode($cookie_data);
        } else {
            // if encryption is not used, we provide an md5 hash to prevent userside tampering
            $cookie_data = $cookie_data . md5($cookie_data . $this->encryption_key);
        }
        
        // Session or persistant cookie...
        
        
        $expiration = $this->sess_expiration + time();
        // We want the cookie to expire when browser session closes
        if ($this->sess_persistant_cookie == FALSE)
        {
            $expiration = 0;
        }
        // Set the cookie
        setcookie($this->sess_cookie_name, $cookie_data, $expiration, $this->cookie_path, $this->cookie_domain, 0);
    }

Simply set your new configuration option to FALSE to have it expire at session close. Enjoy Smile

I just implemented this in my code base and it works like a charm. If the configuration option is set to TRUE all works as normal.


Messages In This Thread
RICK: Any solution yet, to get session expired on browser close? - by El Forum - 03-24-2009, 05:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB