且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

远程销毁php中的会话(用户登录其他地方)?

更新时间:2023-02-24 23:25:36

当然可以,使用 session_id.当用户在其他地方登录时,您可以在为新登录启动新会话之前执行此步骤:

It's certainly possible, using session_id. When the user logs in somewhere else, you can do this step before starting a new session for the new login:

// The hard part: find out what $old_session_id is

session_id($old_session_id);
session_start();
session_destroy();

// Now proceed to create a new session for the new login

这将破坏服务器端的旧会话,因此当另一台计算机再次访问您的应用程序时,它将尝试访问一个不存在的会话,并为其创建一个新会话(用户未登录)在了).

This will destroy the old session on the server side, so when the other computer accesses your application again it will try to access a non-existent session and a new one will be created for it (in which the user is not logged in anymore).

困难的部分是找出旧"会话的 ID 是什么.没有一种万能的方法可以做到这一点.您需要有某种机制来判断 ID 为 XXX 的会话属于现在登录的同一用户.如果您使用数据库会话,这应该很容易.

The hard part is finding out what is the ID of the "old" session. There's no one-size-fits-all way of doing that; you need to have some mechanism in place to be able to tell that the session with id XXX belongs to the same user who is logging in now. If you are using database sessions this should be easy enough.