简体   繁体   English

强制用户注销会话PHP

[英]Force user to logout session PHP

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). 我的登录系统基本上只依赖于包含用户唯一ID(存储在mysql数据库中)的会话。 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. 感谢任何帮助,谢谢。

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. 您需要在每次加载页面时进行检查,或者可能按设定的时间间隔查看Ajax调用以从数据库检查其状态。

Then you can use session_destroy(); 然后你可以使用session_destroy(); to end their session. 结束他们的会议。 This will destroy their entire session. 这会破坏整个会话。

Otherwise you can use unset($_SESSION['user_id']); 否则你可以使用unset($_SESSION['user_id']); to unset a single session variable 取消设置单个会话变量

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. 最好不要每次检查他们查看页面是否他们的状态已被切换为“禁止”,因为这似乎是我服务器上的不必要的压力。

Loading the user from the database on every page load, rather than storing a copy of the user in the session, is a perfectly reasonable solution. 在每次加载页面时从数据库加载用户,而不是在会话中存储用户的副本,这是一个非常合理的解决方案。 It also prevents the user from getting out of sync with the copy in the database (so that, for instance, you can change a user's properties or permissions without them having to log out and back in). 它还可以防止用户与数据库中的副本不同步(例如,您可以更改用户的属性或权限,而无需他们注销并重新登录)。

Try to put this on every page... 试着把它放在每一页......

if (isset($_SESSION['user_id'])) {

    $sql = "SELECT from tbl where status='banned' and user_id=$_SESSION['user_id'] ";
    $query = mysql_query($sql);

    if(!empty(mysql_num_rows($query))){ // found the banned user
       //redirect to logout or
       //session_destroy();
    }

} else {
echo "You need to login!";
}

if the user is still logged in... check if his/her status is banned or not... if banned.. then logout 如果用户仍然登录...检查他/她的状态是否被禁止......如果被禁止..然后注销

你可以取消它。

unset($_SESSION['user_id'])

You could use Custom Session Handlers this way you have full control where and how the session data is stored on the server. 您可以使用自定义会话处理程序,这样您就可以完全控制会话数据在服务器上的存储位置和方式。

So you could store the session data for a particular user in a file called <user_id>.session for example. 因此,您可以将特定用户的会话数据存储在名为<user_id>.session的文件中。 Then, to logout the user, just delete that file. 然后,要注销用户,只需删除该文件。

Ajax calls in an interval will put extra load on server. 间隔中的Ajax调用将在服务器上增加额外负载。 If you want real-time response to your actions(eg the user will be signed out right when you ban them from your system backend), then you should look into something like Server Push . 如果您想要对您的操作进行实时响应(例如,当您从系统后端禁止用户时,用户将被正确注销),那么您应该查看Server Push

The idea is to keep a tunnel open from Server to Browser whenever a user is browsing your website, so that you can communicate with them from server-side too. 这个想法是在用户浏览您的网站时保持从服务器到浏览器的隧道打开,以便您也可以从服务器端与它们进行通信。 If you want them to be banned, push a logout request and the process that in your page(ie force logout by unsetting session). 如果您希望禁止它们,请按下注销请求和页面中的进程(即通过取消设置强制注销)。

This worked for me am using pHP 5.4 include 'connect.php'; 这适用于我使用pHP 5.4包括'connect.php';

  session_start();
  if(session_destroy())
  {
     header("Location: login.php");
  }

You can use session_save_path() to find the path where PHP saves the session files, and then delete them using unlink() . 您可以使用session_save_path()来查找PHP保存会话文件的路径,然后使用unlink()删除它们。

Once you delete the session file stored in the sever, the client side PHPSESSID cookie will no longer be valid for authentication and the user will be automatically be logger out of your application. 删除存储在服务器中的会话文件后,客户端PHPSESSID cookie将不再对身份验证有效,并且用户将自动退出应用程序。

Please be very careful while using this approach, if the path in question turns out to be the global /tmp directory! 使用这种方法时请小心,如果有问题的路径是全局/ tmp目录! There's bound to be other processes other than PHP storing temporary data there. 除了PHP之外,还有其他进程存储临时数据。 If PHP has its own directory set aside for session data it should be fairly safe though. 如果PHP为会话数据留出了自己的目录,那么它应该是相当安全的。

There is a few ways to do this the best in my opinion based on security is: NOTE: THIS IS REALLY ROUGH.... I know the syntax is wrong, its just for you to get an idea. 基于安全性,我认为有几种方法可以做到最好:注意:这真的很粗糙....我知道语法错误,它只是为了让你有个主意。

$con = mysql_connect("localhost","sampleuser","samplepass");
if (!$con)
{
$error = "Could not connect to server";
}
mysql_select_db("sampledb", $con);
$result = mysql_query("SELECT * FROM `sampletable` WHERE `username`='".$_SESSION['user_id']."'");
$userdeets = mysql_fetch_array($result);
if($_SESSION['sessionvalue'] != $userdeets['sessionvalue'])
{
session_destroy();
Header('Location: logout.php');
}
else
{
$result2 = mysql_query("UPDATE `sessionvalue` WHERE `username`='".$_SESSION['user_id']."' SET `sessionvalue` = RANDOMVALUE''");
 $sesval = mysql_fetch_array($result2);
$_SESSION['sessionvalue'] = $seshval
}

Now I know thats not the very code but in essence what you need to do to be secure and have this ability is: 现在我知道这不是代码,但实质上你需要做的是保证安全并具备这种能力:

  • Everytime a page load check a Session value matches a value in the DB. 每次页面加载检查时,会话值都与DB中的值匹配。
  • Every time a page loads set a new session value based on a random generated DB value. 每次页面加载时,都会根据随机生成的DB值设置新的会话值。 you will need to store the username in a session as well. 您还需要在会话中存储用户名。
  • if the Session ID's do not match then you destroy the session and redirect them. 如果会话ID不匹配,则会销毁会话并重定向它们。
  • if it does match you make the new session ID. 如果它匹配,则创建新的会话ID。

if you want to ban a user you can set their sessionvalue in the DB to a value like "BANNED". 如果要禁止用户,可以将DB中的会话值设置为“BANNED”之类的值。 this value will not allow them to log in either. 此值不允许他们登录。 this way you can control user through a simple web form and you can also generate list of banned users very easily etc etc. I wish I had more time to explain it I hope this helps. 通过这种方式,您可以通过简单的Web表单控制用户,您也可以非常容易地生成禁止用户列表等等。我希望我有更多时间来解释它,我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM