0

So I have come across a problem.

 1. I log into website using my user information on my desktop.
 2. My sessions are set within the desktop.
 3. I log into website using a smartphone. (Now there are 2 devices logged in.)
 4. My sessions are set within smartphone.
 5. I change my user information in user settings page using my desktop.
 6. Database is updated.
 7. Sessions are updated on desktop, but not on phone.

How do I fix it? Should I use CodeIgniter's database driver for sessions? Would that even matter?

Thanks.

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
radiantMemory
  • 39
  • 1
  • 10
  • looks like you store your db data in the session too - just add a lastchange timestamp and compare the session data with your db, The other possibility would be - to kick this data out of the session and pick it up on every request – Atural Jan 26 '17 at 12:04
  • @sintakonte so I'd basically have to connect to the database and compare timestamps on every page load? Wouldn't that be kind of inefficient? Or am I being paranoid – radiantMemory Jan 26 '17 at 12:10
  • maybe you are maybe not, but i see your point ;) There are other possibilites - such as installing a service like memcache or redis- in this case - you can use the inbuilt caching driver of CI (https://www.codeigniter.com/userguide3/libraries/caching.html#redis-caching); just save your values to db and point 7 would be - update / insert to your cache system (after that you pick up the data on every request from your cache system) – Atural Jan 26 '17 at 12:43

1 Answers1

0

I just created a session_refresh Model and autoloaded it. Hopefully this can scale and won't impact the server much.

protected $CI;

public function __construct()
{
    $userdata = $this->session->userdata;

    if (isset($userdata['uid']))
    {
        /*
         * CONNECT TO DATABASE AND,
         * CHECK IF MODIFIED TIMESTAMPS CHANGED
         */
        $query = $this->db->get_where('users', array('uid' => $userdata['uid']));
        $row = $query->row();

        /*
         * IF THE "MODIFIED" TIMESTAMP DIFFERS,
         * UPDATE THE SESSION WITH THE NEW DATA.
         */
        if ($userdata['modified'] !== $row->modified)
        {
            $this->CI =& get_instance();
            $this->CI->load->model('user_model');

            $this->CI->user_model->set_user_session($row);
        }
    }
}
radiantMemory
  • 39
  • 1
  • 10