Welcome Guest, Not a member yet? Register   Sign In
Extend CI_Session_database_driver to work with extra cols in ci_sessions table
#1

(This post was last modified: 08-19-2015, 12:08 PM by jLinux.)

I was wondering if there was a way to add some extra custom data to an extra field in the ci_sessions table (when stored in database), I know you can set custom data using set_userdata(), which gets stored as BLOB format in the data column of the ci_sessions table, but I wanted to associate each session with an account ID. Which can be done in the session data, but i wanted to be able to execute a query to show all active sessions associated to an account_id.

I know it can be done with a new table used to associate accounts to sessions, but I was hoping to use the existing table, since it clears the records out when the session is over. Id have to find a way to do the same thing with the association table.

Currently, I have a column in the accounts table that references the session ID, which works fine, as long as the account never has more than one session, I wanted to add some functionality that would show you how many active sessions your account has open in other locations when you login.

Thanks!
Reply
#2

You can do that, yes.

Just, unless you extend the session class, make sure to put a default value for the new column, so INSERTs can go through.
Reply
#3

(This post was last modified: 08-19-2015, 12:04 PM by jLinux.)

How would you recommend doing it? Just extend the class?

What I was thinking was create whatever columns I want in the ci_sessions table, then extend the class, and when a session is created, loop through the data thats handed to it. If theres a column that exists with the same name as the key, then insert that data into the column and remove it from the array.

Then store whatevers left in the array in the data column

Can I extend the CI_Session_database_driver class in the CI way?

MY_CI_Session_database_driver extends CI_Session_database_driver, would that work? Ill give it a go
Reply
#4

(This post was last modified: 08-19-2015, 12:09 PM by jLinux.)

The following doesnt seem to work. I inserted a log_message() just to see if it would log, and tailed the logs, it didnt log it at all.. Am I doing this right? Still sorta new to CI

File: application/core/MY_Session_database_driver.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Session_database_driver extends CI_Session_database_driver {

    public function 
write($session_id$session_data)
    {
        
log_message('error',"SESSTEST: TEST SESSION WRITE: {$session_id}");
        
// Was the ID regenerated?
        
if ($session_id !== $this->_session_id)
        {
            if ( ! 
$this->_release_lock() OR ! $this->_get_lock($session_id))
            {
                return 
FALSE;
            }

            
$this->_row_exists FALSE;
            
$this->_session_id $session_id;
        }
        elseif (
$this->_lock === FALSE)
        {
            return 
FALSE;
        }

        if (
$this->_row_exists === FALSE)
        {
            
$insert_data = array(
                
'id' => $session_id,
                
'ip_address' => $_SERVER['REMOTE_ADDR'],
                
'timestamp' => time(),
                
'data' => ($this->_platform === 'postgre' base64_encode($session_data) : $session_data)
            );

            if (
$this->_db->insert($this->_config['save_path'], $insert_data))
            {
                
$this->_fingerprint md5($session_data);
                return 
$this->_row_exists TRUE;
            }

            return 
FALSE;
        }

        
$this->_db->where('id'$session_id);
        if (
$this->_config['match_ip'])
        {
            
$this->_db->where('ip_address'$_SERVER['REMOTE_ADDR']);
        }

        
$update_data = array('timestamp' => time());
        if (
$this->_fingerprint !== md5($session_data))
        {
            
$update_data['data'] = ($this->_platform === 'postgre')
                ? 
base64_encode($session_data)
                : 
$session_data;
        }

        if (
$this->_db->update($this->_config['save_path'], $update_data))
        {
            
$this->_fingerprint md5($session_data);
            return 
TRUE;
        }

        return 
FALSE;
    }



Renamed thread to a more descriptive title
Reply
#5

That file should be put into application/libraries/Session/drivers/, not application/core/ ...

However, there's very sensitive code in the drivers' files and you'd need to update your extension for every bugfix that they get. I'd consider another solution if I were you.
Reply
#6

(This post was last modified: 08-19-2015, 04:55 PM by jLinux.)

(08-19-2015, 01:44 PM)Narf Wrote: That file should be put into application/libraries/Session/drivers/, not application/core/ ...

However, there's very sensitive code in the drivers' files and you'd need to update your extension for every bugfix that they get. I'd consider another solution if I were you.

Well this is an application that will be bundled up and installed by whoever uses it as a whole, not managed as a website using a CI framework, but rather as an application, all updates will be coming from me, so I would be able to control that.

I think ill create an association table, then just create a wrapper around the write and destroy methods, but not change the methods themselves.

So it just creates the record in the association table or deletes it, then executes the correct method in the CI_Session_database_driver class
Reply




Theme © iAndrew 2016 - Forum software by © MyBB