Skip to content

Commit

Permalink
Session cookie driver changes
Browse files Browse the repository at this point in the history
 - Changed docs CREATE TABLE ci_sessions example to have the PRIMARY KEY of session_id, ip_address and user_agent combined.
 - Changed DB updates to add WHERE clauses for the ip_address and/or user_agent strings if sess_match_ip and/or sess_match_useragent are set to TRUE.
  • Loading branch information
narfbg committed Oct 31, 2012
1 parent 87f4dc2 commit e2afc88
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
36 changes: 31 additions & 5 deletions system/libraries/Session/drivers/Session_cookie.php
Expand Up @@ -540,11 +540,25 @@ protected function _sess_update($force = FALSE)
// Check for database // Check for database
if ($this->sess_use_database === TRUE) if ($this->sess_use_database === TRUE)
{ {
$this->CI->db->where('session_id', $old_sessid);

if ($this->sess_match_ip === TRUE)
{
$this->CI->db->where('ip_address', $this->CI->input->ip_address());
}

if ($this->sess_match_useragent === TRUE)
{
$this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120)));
}

// Update the session ID and last_activity field in the DB // Update the session ID and last_activity field in the DB
$this->CI->db->update($this->sess_table_name, array( $this->CI->db->update($this->sess_table_name,
'last_activity' => $this->now, array(
'session_id' => $this->userdata['session_id'] 'last_activity' => $this->now,
), array('session_id' => $old_sessid)); 'session_id' => $this->userdata['session_id']
)
);
} }


// Write the cookie // Write the cookie
Expand Down Expand Up @@ -590,7 +604,19 @@ public function _update_db()
// Run the update query // Run the update query
// Any time we change the session id, it gets updated immediately, // Any time we change the session id, it gets updated immediately,
// so our where clause below is always safe // so our where clause below is always safe
$this->CI->db->update($this->sess_table_name, $set, array('session_id' => $this->userdata['session_id'])); $this->CI->db->where('session_id', $this->userdata['session_id']);

if ($this->sess_match_ip === TRUE)
{
$this->CI->db->where('ip_address', $this->CI->input->ip_address());
}

if ($this->sess_match_useragent === TRUE)
{
$this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120)));
}

$this->CI->db->update($this->sess_table_name, $set);


// Clear dirty flag to prevent double updates // Clear dirty flag to prevent double updates
$this->data_dirty = FALSE; $this->data_dirty = FALSE;
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/sessions.rst
Expand Up @@ -388,7 +388,7 @@ session class::
user_agent varchar(120) NOT NULL, user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL, last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL, user_data text NOT NULL,
PRIMARY KEY (session_id), PRIMARY KEY (session_id, ip_address, user_agent),
KEY `last_activity_idx` (`last_activity`) KEY `last_activity_idx` (`last_activity`)
); );


Expand Down

0 comments on commit e2afc88

Please sign in to comment.