Permalink
Browse files

Moving database calls out of Session library into sessions model

  • Loading branch information...
1 parent 308692d commit fc660e7ed61a57622c3cf4000016d7c599c44e6c @jtboofle committed Oct 28, 2011
Showing with 62 additions and 18 deletions.
  1. +1 −1 system/core/Loader.php
  2. +14 −17 system/libraries/Session.php
  3. +47 −0 system/models/sessions.php
View
@@ -138,7 +138,7 @@ public function __construct()
$this->_ci_ob_level = ob_get_level();
$this->_ci_library_paths = array(APPPATH, BASEPATH);
$this->_ci_helper_paths = array(APPPATH, BASEPATH);
- $this->_ci_model_paths = array(APPPATH);
+ $this->_ci_model_paths = array(APPPATH, BASEPATH);
$this->_ci_view_paths = array(VIEWPATH => TRUE);
log_message('debug', "Loader Class Initialized");
@@ -96,7 +96,7 @@ public function __construct($params = array())
// Are we using a database? If so, load it
if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')
{
- $this->CI->load->database();
+ $this->CI->load->model('sessions');
}
// Set the "now" time. Can either be GMT or server time, based on the
@@ -210,32 +210,32 @@ function sess_read()
// Is there a corresponding session in the DB?
if ($this->sess_use_database === TRUE)
{
- $this->CI->db->where('session_id', $session['session_id']);
+ $criteria = array('session_id'=>$session['session_id']);
if ($this->sess_match_ip == TRUE)
{
- $this->CI->db->where('ip_address', $session['ip_address']);
+ $criteria['ip_address'] = $session['ip_address'];
}
if ($this->sess_match_useragent == TRUE)
{
- $this->CI->db->where('user_agent', $session['user_agent']);
+ $criteria['user_agent'] = $session['user_agent'];
}
- $query = $this->CI->db->get($this->sess_table_name);
+ $result = $this->CI->sessions->get_session($criteria);
+
// No result? Kill it!
- if ($query->num_rows() == 0)
+ if (!$result)
{
$this->sess_destroy();
return FALSE;
}
// Is there custom data? If so, add it to the main session array
- $row = $query->row();
- if (isset($row->user_data) AND $row->user_data != '')
+ if (isset($result->user_data) AND $result->user_data != '')
{
- $custom_data = $this->_unserialize($row->user_data);
+ $custom_data = $this->_unserialize($result->user_data);
if (is_array($custom_data))
{
@@ -297,8 +297,7 @@ function sess_write()
}
// Run the update query
- $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->CI->sessions->update_session($this->userdata['session_id'],array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
// Write the cookie. Notice that we manually pass the cookie data array to the
// _set_cookie() function. Normally that function will store $this->userdata, but
@@ -337,7 +336,7 @@ function sess_create()
// Save the data to the DB if needed
if ($this->sess_use_database === TRUE)
{
- $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
+ $this->CI->sessions->create_session($this->userdata);
}
// Write the cookie
@@ -393,7 +392,7 @@ function sess_update()
$cookie_data[$val] = $this->userdata[$val];
}
- $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
+ $this->CI->sessions->update_session($old_sessid,array('last_activity' => $this->now, 'session_id' => $new_sessid));
}
// Write the cookie
@@ -413,8 +412,7 @@ function sess_destroy()
// Kill the session DB row
if ($this->sess_use_database === TRUE AND isset($this->userdata['session_id']))
{
- $this->CI->db->where('session_id', $this->userdata['session_id']);
- $this->CI->db->delete($this->sess_table_name);
+ $this->CI->sessions->delete_session($this->userdata['session_id']);
}
// Kill the cookie
@@ -790,8 +788,7 @@ function _sess_gc()
{
$expire = $this->now - $this->sess_expiration;
- $this->CI->db->where("last_activity < {$expire}");
- $this->CI->db->delete($this->sess_table_name);
+ $this->CI->sessions->purge($expire);
log_message('debug', 'Session garbage collection performed.');
}
View
@@ -0,0 +1,47 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+
+class Sessions extends CI_Model {
+ var $sess_table_name;
+
+ function __construct()
+ {
+ parent::__construct();
+ $this->load->database();
+ $this->sess_table_name = $this->config->item('sess_table_name');
+
+ }
+
+ function get_session($criteria)
+ {
+ $query = $this->db->get_where($this->sess_table_name,$criteria);
+ if ($query->num_rows()==0)
+ {
+ return FALSE;
+ }
+ return $query->row();
+ }
+
+ function update_session($session_id, $data)
+ {
+ $this->db->where('session_id', $session_id);
+ $this->db->update($this->sess_table_name, $data);
+ }
+
+ function create_session($data)
+ {
+ $this->db->query($this->db->insert_string($this->sess_table_name, $data));
+ }
+
+ function delete_session($session_id)
+ {
+ $this->db->where('session_id', $session_id);
+ $this->db->delete($this->sess_table_name);
+ }
+
+ function purge($expire)
+ {
+ $this->db->where("last_activity < {$expire}");
+ $this->db->delete($this->sess_table_name);
+ }
+
+}

0 comments on commit fc660e7

Please sign in to comment.