从其他用户销毁会话

在我更改了其他用户的权限或禁止状态后,我尝试从其他用户那里销毁会话...


我将每个用户的会话密钥写入数据库,并在访问 Profiles 时捕获它们。如果有任何更改,我希望用户立即被踢出系统......


这是我的代码:



    FUNCTION back_to_home() {

            mysqli_close($db);

            $session_id_to_destroy = $_SESSION['visit_user-session_id'];

            session_id($session_id_to_destroy);

            session_start();

            session_destroy();

            unset($_SESSION['visit_user-username']);

            unset($_SESSION['visit_user-e_mail']);

            unset($_SESSION['visit_user-register_date']);

            unset($_SESSION['visit_user-last_login_date']);

            unset($_SESSION['visit_user-register_ip']);

            unset($_SESSION['visit_user-last_login_ip']);

            unset($_SESSION['visit_user-steam_id']);

            unset($_SESSION['visit_user-permissions']);

            header('Location: ../../../');

            exit;

        }


我希望我可以用 PHP 修复或做到这一点。我不知道 JavaScript xD


所以我想知道的是,我可以那样做还是有另一种方法可以从他的会话中踢出另一个用户?


当年话下
浏览 125回答 1
1回答

慕桂英4014372

我尝试从其他用户销毁会话从技术上讲,您可以做到这一点,是的,但是这个过程冗长且有问题。如何删除任意会话。A:查找会话您需要使用该session_id值。该值是文件名的一部分(或者在数据库会话的情况下是会话标识符列值)。您需要知道文件名前体(通常sess_但可以是 PHP 源代码中设置的任何内容)。您还需要知道 PHP.ini 文件中设置的会话存储位置。例子:带有 id 的会话58ce93c623376b3ddfca3cfc3a01d57d3be85084363147464是一个位于以下位置的文件:/home/session_storage_folder/sess_58ce93c623376b3ddfca3fc3a01d57d3be85084363147464但是会话文件名是即时生成的,不会(也不应该)连接到您的会员数据库中的 who 。如果您手动生成会话 id,那么这会变得更容易,但是会话的安全性会大大降低,如果没有非常仔细的考虑,这应该真的,真的不能这样做。B:寻找用户现在您需要找到要禁止的用户。会话文件将包含用户 ID,不知何故,会话数据通常存储为:(例子)$_SESSION['alpha'] = "UiOvMfV9byatH4Wt1SPYUO3zgsj5";$_SESSION['beta']&nbsp; =&nbsp; 1324;&nbsp;alpha|s:28:"UiOvMfV9byatH4Wt1SPYUO3zgsj5";beta|i:1324;这是[变量名称]|[变量类型]:[变量内容长度]:[内容数据]; [等等....]因此,如果您user id设置了如下值$_SESSION['user_id'] = 45;:用户 ID|i:45;在会议中。因此,您需要在每个会话中搜索此数据字符串。请阅读有关如何执行此操作的问题所以你会有这样的代码:$string = 'user_id|i:".(int)$user_id_to_block;$session_file_name = null;foreach (glob('/home/session_folder/*') as $file) {&nbsp; &nbsp; $content = file_get_contents("/home/session_folder/".$file);&nbsp; &nbsp; if (strpos($content, $string) !== false) {&nbsp; &nbsp; &nbsp; &nbsp; $session_file_name = "/home/session_folder/".$file;&nbsp; &nbsp; }}找到后,您可以在服务器上删除该会话。if(file_exist($session_file_name)){&nbsp; &nbsp; unlink($session_file_name);}但:对于许多会话,这将非常缓慢且效率低下。你应该怎么做您应该检查登录用户的每个页面加载是否经过身份验证。假设您的用户详细信息是数据库驱动的,您应该检查每个页面加载的详细信息是否真实。<?phpsession_start();if($_SESSON['user_id'] > 0){&nbsp; &nbsp; &nbsp;/////&nbsp; &nbsp; &nbsp;$sql = "SELECT banned FROM users WHERE user_id = :user_id";&nbsp;&nbsp; &nbsp; &nbsp;/////&nbsp; &nbsp; &nbsp;// Etc. etc.&nbsp;&nbsp; &nbsp; &nbsp;$result = get MySQL result;&nbsp; &nbsp; &nbsp;if($result['banned'] === 'Y'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /***&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Member is banned. kick them out.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;***/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$_SESSION = []; // reset session.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header("Location: index.php");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;die();&nbsp;&nbsp; &nbsp; &nbsp;}}更新如果您使用会话 ID 作为标识符并且您知道会话 ID 而无需搜索它;只需这样做:FUNCTION back_to_home() {&nbsp; &nbsp; &nbsp; &nbsp; mysqli_close($db);&nbsp; &nbsp; &nbsp; &nbsp; // save current admin session (optional).&nbsp; &nbsp; &nbsp; &nbsp; $admin_session = session_id();&nbsp; &nbsp; &nbsp; &nbsp; // get target id.&nbsp; &nbsp; &nbsp; &nbsp; $session_id_to_destroy = $_SESSION['visit_user-session_id'];&nbsp; &nbsp; &nbsp; &nbsp; // close the current session.&nbsp; &nbsp; &nbsp; &nbsp; session_write_close();&nbsp; &nbsp; &nbsp; &nbsp; // load the specified target session&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; session_id($session_id_to_destroy);&nbsp; &nbsp; &nbsp; &nbsp; // start the target session.&nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp; &nbsp; &nbsp; &nbsp; // clean all session data in target session.&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION = [];&nbsp; &nbsp; &nbsp; &nbsp; // save and close that session.&nbsp; &nbsp; &nbsp; &nbsp; session_write_close();&nbsp; &nbsp; &nbsp; &nbsp; // Optional if you need to resume admin session:&nbsp; &nbsp; &nbsp; &nbsp; // reload admin session id&nbsp; &nbsp; &nbsp; &nbsp; session_id($admin_session);&nbsp; &nbsp; &nbsp; &nbsp; // restart admin session. . ..&nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // header should go to a specific file.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; header('Location: ../index.php');&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP