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 auser_id
column in myci_session
table then when a user logs in use thatuser_id
to check all theuser_id
in the sessions table and then delete those sessions then add thatuser_id
to the new session. This what I have done so far:
Step 1
Create a columnuser_id
inci_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.
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CodeIgniter is a framework that is known for requiring a minimum amount of customization to get it up and running. This allows those who choose it to work at a good pace. It has been updated many times since its inception in 2006. Now the most recent version is 4.0.3.
https://www.codeigniter.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.