如何“到处注销"?在PHP中工作? [英] How does "Logout Everywhere" work in PHP?

查看:75
本文介绍了如何“到处注销"?在PHP中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现stackoverflow有一个链接,用于注销每台已登录的计算机.

I just found out that stackoverflow has a link to logout every logged computers.

所以..我想到了如何在PHP中实现相同的功能.我想出了使用session_set_save_haldner来控制write()方法.在write()方法中,我可以使会话文件以用户的用户名开头.例如,用户john可能具有会话文件john_kdkajdkak和john_29039dla.当John单击注销到处"时,我可以编写代码以找到以"john"开头的文件名,然后将其删除以清除会话.

So..I thought about how to implement same functionality in PHP. I came up with using session_set_save_haldner to control write() method. In write() method, I can make a session file start with user's username. For example, a user john might have session files john_kdkajdkak, and john_29039dla. When John clicks "Logout Everywhere", I can write a code that finds filenames start with "john" then remove them to clear sessions.

还有其他更好的解决方案吗?如果您已经实现了它,那么如何实现呢?

are there any other better solutions? How did you implement it if you already made it work?

推荐答案

使用数据库来保留会话数据.

Use a database to persist session data.

使用session_set_save_handler您可以为用户会话滚动自己的数据库存储后端-一个具有 user_id 外键且与 users 表相关的 sessions .一个到处注销"按钮将触发简单的 DELETE FROM会话,其中user_id = 1234 ,并使该用户的每个会话无效.

Using session_set_save_handler you can roll your own database storage backend for user sessions - a sessions that has a user_id foreign key, related to the users table. A "logout everywhere" button would trigger simple DELETE FROM sessions WHERE user_id = 1234 and invalidate every session for the user.

您还可以轻松地在 session 表中添加其他列-例如,存储会话的IP地址,以便用户可以看到其他会话从何处登录.

You can also easily add in additional columns to the session table - to store the IP address of the session, for instance, so users can see where other sessions are logged in from.

使用数据库来提高灵活性和性能.

Use a database for flexibility and performance.

这篇关于如何“到处注销"?在PHP中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆