常见的请求或建议是Web应用程序不允许用户一次激活多个会话。换句话说,在用户登录到应用程序之后,不应允许他打开其他类型的浏览器(或使用其他计算机)再次登录,直到他的第一个会话结束。如果用户尝试再次登录,则该应用程序应使先前的会话到期。我试图实现这一壮举无济于事。任何帮助或建议,将不胜感激。
顺便说一句:我的PHP Codeigniter背景不是很稳定。
我尝试使用此处的教程
https://blog.lucideus.com/2018/03/prevent-multiple-concurrent-logins-in.html
它建议一个解决方案,但是当我尝试时,出现HTTP错误500
我的控制器:
function validate_login() {
$email = $this->input->post('email');
$password = $this->input->post('password');
$credential = array('email' => $email, 'password' =>
sha1($password));
// Checking login credential for staff
$query = $this->db->get_where('staff', $credential);
if ($query->num_rows() > 0) {
$row = $query->row();
$this->session->set_userdata('staff_login', '1');
$this->session->set_userdata('login_user_id', $row-
>staff_id);
$this->session->set_userdata('name', $row->name);
$this->session->set_userdata('login_type', 'staff');
redirect(site_url('staff/dashboard'), 'refresh');
}
$clientstatus['status'] =NULL;
$credentialandstatus = array_merge($credential,$clientstatus);
// Checking login credential for client
$query = $this->db->get_where('client', $credentialandstatus);
if ($query->num_rows() > 0) {
$row = $query->row();
$this->session->set_userdata('client_login', '1');
$this->session->set_userdata('login_user_id', $row-
>client_id);
//custom code for preventing multiple concurrent logins
$sessionData = $this->session->all_userdata();
$client_id = $sessionData['client_id'];
$sessionId=session_id();
$this->crud_model->setSession($client_id, $sessionId);
//ends
$this->session->set_userdata('name', $row->name);
$this->session->set_userdata('login_type', 'client');
$client_fund = $this->crud_model->get_fund($row-
>client_id);
$this->session->set_userdata('fund', $client_fund);
redirect(site_url('client/dashboard'), 'refresh');
}
$this->session->set_flashdata('login_error',
get_phrase('invalid_login'));
redirect(site_url('login'), 'refresh');
}
我的模型很简单:
function setSession($client_id, $sessionId){
//Get previous mapped session ID
$oldSessionId = $this->db-select("session_id")-
>where(array('client_id'=>$client_id))->get("client")-
>row('session_id');
//Destroy session which was mapped to previous user
$this->db-where('id',array('id'=>$oldSessionId));
$this->db->delete('ci_sessions', $sessionData);
//Map new session ID to the new user
$this->db->where('client_id', $client_id);
$this->db->update('client', array('session_id'=>$session_id));
}
我想要的结果如标题中所述。