强制用户注销会话PHP [英] Force user to logout session PHP

查看:73
本文介绍了强制用户注销会话PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到这个问题的直接答案.有没有一种方法可以强制已登录的用户注销?我的登录系统基本上仅依赖于包含用户唯一ID(存储在mysql数据库中)的会话.所以本质上就是……

I can't seem to find a straightforward answer to this question. Is there a way in which I can force a logged in user to logout? My login system essentially just relies on a session containing the user's unique ID (which is stored in a mysql database). So essentially just...

if (isset($_SESSION['user_id'])) {
echo "You're logged in!";
} else {
echo "You need to login!";
}

但是,假设我要禁止该用户,那么我可以将其状态更改为数据库中的禁止",但是直到用户注销并尝试重新登录后,此操作才起作用.强制该用户注销?最好不要每次都查看页面,而是查看它们的状态是否已切换为禁止",因为这似乎对我的服务器造成不必要的压力.感谢您的任何帮助,谢谢.

But let's say I want to ban this user, well I can change their status to banned in my database but this won't do anything until the user logs out and attempts to log back in... So, how do I force this user to logout? Preferably without checking every single time they view a page whether or not their status has been switched to "banned" because that seems like unnecessary stress on my server. Any help is appreciated, thank you.

推荐答案

您需要在每次加载页面时进行检查,或者可能需要按设置的时间间隔查看Ajax调用以从数据库中检查其状态.

Either you need to check every time they load a page, or possibly look at an Ajax call at set intervals to check their status from the DB.

然后您可以使用session_destroy();结束他们的会话.这将破坏他们的整个会话.

Then you can use session_destroy(); to end their session. This will destroy their entire session.

否则,您可以使用unset($_SESSION['user_id']);取消设置单个会话变量

Otherwise you can use unset($_SESSION['user_id']); to unset a single session variable

这篇关于强制用户注销会话PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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