CodeIgniter Forums
Max concurrent sessions per user problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Max concurrent sessions per user problem (/showthread.php?tid=55324)



Max concurrent sessions per user problem - El Forum - 10-20-2012

[eluser]pablini[/eluser]
Hi!
I'm working on a project that needs a maximun concurrent sessions per user and i'm having problems implementing it.

Right now my config saves sessions on database and my controller checks for how many users with the same id are logged in. The problem appears to be that codeigniter never deletes the old sessions (when you close your browser/tab).

My current config for sessions is:
Code:
$config['sess_cookie_name']  = 'ci_session';
$config['sess_expiration']  = 100;
$config['sess_expire_on_close'] = true;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name']  = 'sessions';
$config['sess_match_ip']  = true;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 1;

and my controller looks like this:

Code:
public function index(){
    if(!($this->session->userdata('logged_in'))){
        //Field validation failed.  User redirected to login page
        $this->load->view('user_login');
    }else{
        //Go to private area
        if(!($this->isAllowed())){
            $this->load->view('max_users');
        }else if($this->hasExpired()){
            $this->load->view('account_suspended');
        }else{
     $this->load->view('home');
}
    }
}
private function hasExpired(){
            $this->load->model('User');
            $u = new User();
            $uId = $this->session->userdata('user_id');
            
            $u->where('id', $uId)->get();
            
            return ($u->days_left <= 0);
        }
        private function isAllowed(){
            $this->load->model('User');
            $this->load->model('Suscription');
            
            $u = new User();
            $s = new Suscription();
            $uId = $this->session->userdata('user_id');
            
            $u->where('id', $uId)->get();
            $s->where('id', $u->suscription_type)->get();
            
            $maxUsers = $s->users_max;
            
            $query=$this->db->query('SELECT * FROM sessions where user_id='.$uId);

            $count=$query->num_rows();

            return $count <= $maxUsers;
        }

An example of the problem is:
User with max concurren session = 1

I log in the site with PC #1
Close the tab/browser
I log in the site with PC #2 -> Error: max users online
I log in (again) with PC#1 -> Error: max users online

Any ideas ???

thanks!


Max concurrent sessions per user problem - El Forum - 10-20-2012

[eluser]GDmac - expocom[/eluser]
1. the old session data is cleared out periodically, (maybe also when you log out manually)

the solution i saw in another app is to not just fail the new login, but to
ask if the user knows that there is another session still active,
and wether you want to continue logging in and close that other session.



Max concurrent sessions per user problem - El Forum - 10-20-2012

[eluser]pablini[/eluser]
That would work but the thing is where you get redirected after a valid login is to a flash app (for little kids) and my client wants to have this kind of filter mainly because his buisness logic (if you want more concurrent sessions pay more).


Max concurrent sessions per user problem - El Forum - 10-20-2012

[eluser]GDmac - expocom[/eluser]
still the same, on re-login, ask or say, and then remove other sessions.


Max concurrent sessions per user problem - El Forum - 10-21-2012

[eluser]pablini[/eluser]
i canĀ“t do that Sad (remember this site is for kids under 5) what i need is more control on the session table some kind of nicer session flush