php - Codeigniter 3 stop multiple logins using ci_sessions database

I am working with this app made in codeigniter 3 and I'm trying to stop concurrent logins from occurring. Since by default CI doesn't have something like this I've decided to try and write a few functions to achieve this. I haven't done too much but this is my overall plan and would like opinions and thoughts on my approach. What I think i should do is create a user_id column in my ci_session table then when a user logs in use that user_id to check all the user_id in the sessions table and then delete those sessions then add that user_id to the new session. This what I have done so far:

Step 1

Create a column user_id in ci_sessions

I don't think this column has to be a key to the user table since we're just comparing it to the user currently logging in. Here is what the table looks like now after the update.

id | ip_address | timestamp | data | user_id

Step 2

So now that this is done the next step is to add user ID to the session when they log in. So within the login flow if the login is successful we can compare this user ID to all the current user id sessions and if there are any that have the same user ID we delete the session.

Step 3

Store the current user id into the session

I think these are the best steps unless somebody knows a better way to handle concurrent logins with codeigniter.

Answer

Solution:

The way I did it was absolutely correct. Fortunately for me all the controller changes I needed to make were all in the folder and it was super simple to make the adjustments.

This is what the login validation flow looks like:

    public function validate_login($from = "") {
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $credential = array('email' => $email, 'password' => sha1($password), 'status' => 1);

        // Checking login credential for admin
        $query = $this->db->get_where('users', $credential);

        if ($query->num_rows() > 0) {
            $row = $query->row();
            $this->session->set_userdata('user_id', $row->id);
            $this->session->set_userdata('role_id', $row->role_id);
            $this->session->set_userdata('role', get_user_role('user_role', $row->id));
            $this->session->set_userdata('name', $row->first_name.' '.$row->last_name);
            $this->delete_session_user_id();
            $this->session->set_flashdata('flash_message', get_phrase('welcome').' '.$row->first_name.' '.$row->last_name);
            if ($row->role_id == 1) {
                $this->session->set_userdata('admin_login', '1');
                redirect(site_url('admin/dashboard'), 'refresh');
            }else if($row->role_id == 2){
                $this->session->set_userdata('user_login', '1');
                $this->set_session_user_id();
                redirect(site_url('home/my_courses'), 'refresh');
            }
        }else {
            $this->session->set_flashdata('error_message',get_phrase('invalid_login_credentials'));
            redirect(site_url('home/login'), 'refresh');
        }
    }

And these are the extra functions i added to complete this

    public function delete_session_user_id(){
        $session_user_id = $this->session->userdata('user_id');
        $this->db->where('user_id',$session_user_id);
        $this->db->delete('ci_sessions');
    }

    public function set_session_user_id() {
        $session_user_id = $this->session->userdata('user_id');
        $this->db->set('user_id',$session_user_id);
        $this->db->where('id', session_id());
        $this->db->update('ci_sessions');
    }

All in all was actually a lot simpler than I thought

Source