Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Moving database calls out of Session library into sessions model #632

Closed
wants to merge 1 commit into from

5 participants

@jtboofle

For those of us with 'special' database needs (in my case, connecting to DB2 and using non-standard field names in the session table), having all the database calls embedded in the session class is a pain in the keister. I've moved them out into a sessions model which makes fiddling around with session handling a simple matter of overriding 5 function calls.

@gaker

hrm, I see your point, however, would good support for DB2 be better? How are you working with DB2 now, is that something you'd like to see about contributing back?

@jtboofle

Support for DB2 would be great, though my situation is probably pretty unusual -- I doubt too many people are running CI on an IBM iSeries. I actually ended up making native database calls (it looks a lot like PDO, but even more of a pain) and not using the ActiveRecord tools at all. I might try to wrap my code in something that's more general-purpose, but right now it's not fit for public consumption.

Moving the database code out helps everyone who has funky database issues. I wrote (and re-wrote) my data access libraries repeatedly trying to get something that would work well on the iSeries but once I had the database calls moved out of the session class, I didn't have to muck around in there again. I just overrode the model.

@qrool

@jtboofle
I`ve got similar thing I run some apps on Tomcat/Quercus env. I need to have an access to java backend because we have SOA env. running lots of hubs and adapters with heavy data load.
In my case I have php-java mapper/driver extended by models (where java backend deals with DB using hibernate) Updating the session table is done by Auth model (it loads user_session_model), I gave up with updating user_session table using Session lib.

@timw4mail timw4mail commented on the diff
system/libraries/Session.php
((22 lines not shown))
// No result? Kill it!
- if ($query->num_rows() == 0)
+ if (!$result)

Picky, but the style guide says to surround ! with spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg
Owner

The Session class has been drastically changed since this PR was submitted and to be honest - I don't think this is the right approach at all.
Also, DB2 is supported via PDO in develop.

@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  system/core/Loader.php
@@ -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");
View
31 system/libraries/Session.php
@@ -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)

Picky, but the style guide says to surround ! with spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
{
$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
47 system/models/sessions.php
@@ -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);
+ }
+
+}
Something went wrong with that request. Please try again.