Welcome Guest, Not a member yet? Register   Sign In
Session Lost On Successive HTTP Requests
#1

[eluser]wakextreme[/eluser]
I am currently working on a system using CI and ExtJs. The entire system relies totally on ExtJs as the UI and CI as be backend. Because of this sometimes multiple ajax http requests are sent to the server successively. CI handles this fine until it is time to regenerate the session data. The requests sent at the same time causes CI to lose the session. Example:

Session Config:
Code:
$config['sess_cookie_name']        = 'session';
$config['sess_expiration']        = 7200;
$config['sess_encrypt_cookie']    = TRUE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']        = 'sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent']    = TRUE;
$config['sess_time_to_update']  = 1;

A user clicks on a link that submits 3 http requests to the server back to back. The first request hits the server and CI sees that its time to update the session data. CI successfully updates both the browser cookie and the database data. The problem lies in that the second http request grabbed the cookie before the first request made the session ID updates. The second request gets the session ID from the cookie and then tries to find a matching session in the database but ooooops the first request has already updated the db. The second request cannot find a matching session in the database and therefore thinks that the session is invalid and destroys the session.

Code:
// Is there a corresponding session in the DB?
        if ($this->use_database === TRUE)
        {
            $this->CI->db->where('session_id', $session['session_id']);
                    
            if ($this->CI->config->item('sess_match_ip') == TRUE)
            {
                $this->CI->db->where('ip_address', $session['ip_address']);
            }

            if ($this->CI->config->item('sess_match_useragent') == TRUE)
            {
                $this->CI->db->where('user_agent', $session['user_agent']);
            }
            
            $query = $this->CI->db->get($this->session_table);

            if ($query->num_rows() == 0)
            {
                $this->sess_destroy();
                return FALSE;
            }
            else
            {
                $row = $query->row();
                if (($row->last_activity + $this->sess_length) < $this->now)
                {
                    $this->CI->db->where('session_id', $session['session_id']);
                    $this->CI->db->delete($this->session_table);
                    $this->sess_destroy();
                    return FALSE;
                }
            }
        }

If db sessions are turned off the problem does not exist.




Theme © iAndrew 2016 - Forum software by © MyBB