Welcome Guest, Not a member yet? Register   Sign In
Max concurrent sessions per user problem
#1

[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!


Messages In This Thread
Max concurrent sessions per user problem - by El Forum - 10-20-2012, 10:24 PM
Max concurrent sessions per user problem - by El Forum - 10-20-2012, 10:37 PM
Max concurrent sessions per user problem - by El Forum - 10-20-2012, 10:44 PM
Max concurrent sessions per user problem - by El Forum - 10-20-2012, 11:18 PM
Max concurrent sessions per user problem - by El Forum - 10-21-2012, 12:13 AM



Theme © iAndrew 2016 - Forum software by © MyBB