Welcome Guest, Not a member yet? Register   Sign In
[FIXED w/ Code] CI Sessions
#1

[eluser]AnimeCYC[/eluser]
Hello,

Having trouble with CI cessions, for some reason it creates a new session on every page load and doesn't update the previous one; thus making it impossible to retrieve set data with either the set_userdata or set_flashdata methods and retrieve it. Does anyone have a clue as to why this is happening?

UPDATE ::
Ok here is the fix, instead of setting the expire on the cookie it is much easier and better to set the max age, this should work across all browsers (yes even IE). Let me know if there are any issues.

UPDATE 2 ::
Updated to fix an issue when the sessions gets re-written.

Code:
<?php
    class MY_Session extends CI_Session {        
        function sess_write() {
            if ($this->sess_use_database === FALSE) {
                $this->_set_cookie();
                return;
            }

            $custom_userdata = $this->userdata;
            $cookie_userdata = array();

            foreach(array('session_id','ip_address','user_agent','last_activity') AS $val) {
                unset($custom_userdata[$val]);
                $cookie_userdata[$val] = $this->userdata[$val];
            }

            if(count($custom_userdata) === 0) {
                $custom_userdata = '';
            } else {
                $custom_userdata = $this->_serialize($custom_userdata);
            }

            $this->CI->db->where('session_id', $this->userdata['session_id']);
            $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
            $this->_set_cookie($cookie_userdata);
        }

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

            $cookie_data = $this->_serialize($cookie_data);

            if ($this->sess_encrypt_cookie == TRUE) {
                $cookie_data = $this->CI->encrypt->encode($cookie_data);
            } else {
                $cookie_data = $cookie_data . md5($cookie_data.$this->encryption_key);
            }

            $this->_set_cookie_age($this->sess_cookie_name, $cookie_data, $this->sess_expiration, $this->cookie_path, $this->cookie_domain);
        }

        function _set_cookie_age($cookieName, $data = '', $age = 0, $path = '', $domain = '', $secure = FALSE, $http = FALSE) {
            header('Set-Cookie: ' . rawurlencode($cookieName) . '=' . rawurlencode($data)
                . ($age < 1 ? '' : '; Max-Age=' . $age)
                . (empty($path) ? '' : '; path=' . $path)
                . (empty($domain) ? '' : '; domain=' . $domain)
                . (!$secure ? '' : '; secure')
                . (!$http ? '' : '; HttpOnly'), FALSE);
        }
    }
#2

[eluser]Colin Williams[/eluser]
It is improperly configured. Sometimes you can't use the match user agent or ip options. Tweak it around and find what works. It varies between servers. Better yet, learn about your server and know exactly what configuration will work.
#3

[eluser]AnimeCYC[/eluser]
[quote author="Colin Williams" date="1256280900"]It is improperly configured. Sometimes you can't use the match user agent or ip options. Tweak it around and find what works. It varies between servers. Better yet, learn about your server and know exactly what configuration will work.[/quote]

Thanks, i'll give it a try.
#4

[eluser]AnimeCYC[/eluser]
Ok, I can confirm that all combinations of what you mentioned do not work. I really need this working, any other ideas?

UPDATE ::
Alright after getting pissed off at the sessions I dug into the Session class to figure out the problem and found it. The session class uses the local time of the server and thus if the timezones don't match on the client side the cookie will be terminated immediately as it is created. I will post a fix for this as soon as I make it.

UPDATE 2
I added the code to the first post and updated the title.

UPDATE 3
Fixed an issue with writing to the session.




Theme © iAndrew 2016 - Forum software by © MyBB