問題描述
如何刪除特定用戶的會話登錄 (How to remove session login for specific user)
我正在為每個用戶創建一個登錄會話。這是我的會話功能:
public function check_login()
{
$uname = $this‑>input‑>post('username');
$passwd = $this‑>input‑>post('password');
$this‑>form_validation‑>set_rules('username','user_name','required');
$this‑>form_validation‑>set_rules('password','password','required');
if( $this‑>form_validation‑>run() == false )
{
$this‑>load‑>view('login');
}
else
{
$this‑>load‑>model('model_login');
$result = $this‑>model_login‑>query_login();
if(!$result)
{ $this‑>load‑>view('login'); }
else
{
//Log History
$id_user = $this‑>session‑>userdata('id_user');
$data_log['id_user'] = $this‑>session‑>userdata('id_user');
$data_log['log_time'] = date("Y‑m‑d H:i:s");
$data_log['status'] = 1;
$this‑>model_login‑>insert_log($data_log);
$data = array( 'log_status' => 1 );
$this‑>model_login‑>update_log($data,$id_user);
echo "<script>alert('Login Sukses')</script>";
redirect(base_url() . 'con_vendor/index');
}
}
}
當用戶登錄或註銷時,他們的歷史記錄被記錄到數據庫中(參見日誌歷史註釋)並且他們的登錄狀態更改為 1(這意味著他們在線)。
但是,當用戶註銷時,似乎所有用戶的會話數據都被刪除,而不僅僅是註銷的用戶。在 user_log db 中,僅記錄第一個用戶註銷 ID。下一個用戶ID雖然記錄了註銷時間,但不記錄。
例如:
用戶A登錄,然後用戶B登錄。所有登錄數據都記錄到 user_log 表中。然後用戶 B 註銷,然後用戶 A 註銷。只記錄用戶 B 的會話數據,用戶 A 的會話數據丟失。
這是我的註銷功能:
參考解法
方法 1:
you need to save the sessions into the database and check client session (code to the header.php) if session from database still available the user can be on but if the session has been deleted redirect the user to the logout page. Notice : you can remove the session from the admin panel. ( just a simple mysql code)
(by Vahn、Mohsen TOA)