定时自动注销和浏览器关闭 [英] timed auto logout and browser close

查看:195
本文介绍了定时自动注销和浏览器关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于学习目的,我已经创建了一个非常简单的多用户游戏.

I've created a very simple multiuser game for learning purposes.

用户登录时,每个其他用户都会获得当前所有已登录用户的更新.

As users log on, each other user gets an update of all currently logged in users.

用户登录时,只需将SQL数据库中该用户的值设置为1.当他们注销时,该值应为0.

When a user logs in, it simply sets a value for that user in the SQL database to 1. When they're logged out, the value should be 0.

我正在使用$(window).unload(function(){});尝试捕获选项卡/浏览器关闭,但这仅能正常工作.

I'm using $(window).unload(function() {}); to try to catch tab/browser closes, but it only sortof works.

两个问题:

  1. 是否有更好的方法捕获浏览器或选项卡关闭?

  1. Is there a better way to catch browser or tab close?

如果它错过了选项卡的关闭,或者他们的机器崩溃了,或者互联网连接中断了,或者用户只是离开了机器,我想继续并自动注销它们.

In the event that it misses the tab close, or their machine crashes, or internet connection dies, or the user simply walks away from the machine, I want to go ahead and log them out automatically.

我正在将HTML/Jquery前端与PHP后端一起使用.我该怎么做才能解决第二个问题?我假设我需要在PHP中执行此操作..我们在假设浏览器可能不再存在的情况下工作,因此无法处理jquery内容. PHP可以在连续计时器上执行某项操作来检查用户是否还在吗……而不必简单地让用户每10秒单击一次按钮?

I'm using an HTML/Jquery frontend with PHP backend. What can I do to accomplish the second question? I assume I need to do it in PHP.. we're working under the assumption that the browser is likely no longer even around, hence not processing jquery stuff. Can PHP do something on an continuous timer that checks to see if the user is still around... without simply having the users click a button every 10 seconds?

这里有一个潜在的解决方案:如何检测用户是否已经以php登出了? 但是我正在使用ajax来避免页面刷新.理想情况下,用户将从不翻页或单击任何按钮(我正在测试,请记住,这不是真正的应用程序). PHP是否会在没有刷新整个页面的情况下看到上一个活动?

There's a potential solution here: How to detect if a user has logged out, in php? But I'm using ajax to avoid page refreshes. Ideally, the user will never f5 the page, or click any buttons (I'm testing, remember, this is not a for real app). Will PHP see last activity without a full page refresh?

Edit2: 我已经将以下代码添加到我的PHP中,并使用setInterval

I've added the following code to my PHP, with a corresponding jquery function using setInterval

if (isset ($_POST['keepalive'])) {
    if (filter_input(INPUT_POST,'keepalive') == '1') {
        $name = $_SESSION['name'];
        $time = time();
        mysql_query("UPDATE tictac_names SET keep_alive = '$time' WHERE name ='$name'") or die(mysql_error());
    }   
}

这会将unix epoc时间戳插入到我的表中,这对于简单的计算将非常容易.

This plugs a unix epoc timestamp into my table, which will be super easy for simple calculations.

我现在的问题是:如何告诉PHP对每位登录用户X秒运行检查?我的PHP后端文件主要是设置为捕获发布变量并运行代码,然后将其交还给jquery.由于此代码旨在注销不活动的浏览器/用户,因此我不能依靠jquery向PHP发送请求,并且没有刷新PHP.我是否需要做某种Cron作业或某种怪异操作才能使PHP检查最近X秒钟内未更新的所有用户?

My question now is: How do I tell PHP to run a check for each logged in user ever X number of seconds? My PHP backend file is primarily just set to catch post variables and run code, then hand it back to jquery. Since this code is intended to log out inactive browsers/users, I can't rely on jquery sending a request to PHP, and there's no refresh of the PHP. Do I need to do some kind of cron job or some bizarreness to get PHP to check for all users who have not updated in the last X seconds?

帮助!

推荐答案

我假设用户在注销时显示为联机和脱机.销毁会话或cookie将需要客户端浏览器处于工作模式.

I assume that, users are shown as online and offline on logout. Destroying session or cookie will require client browser in working mode.

解决方案

我还假设维护了一个timestamp列.设置时间间隔以检查当前TS和上一个时间戳之间的时间间隔.还要根据您的ID将自己的TS更新为当前的TS.

I also assume there is a timestamp column maintained. Set an interval to check time-gap between current TS and Last Timestamp. Also update your own TS against your id to current TS.

setInterval(function(){ 
    $.ajax({
        url: "/backend.php?userid=id",
        success: function(resoponse){
            console.log(response);
        }
    });
}, 10000);

backend.php

backend.php

$user_id = $_GET["userid"];
$query = "select * from table name where (GETDATE() - timestamp) > 60";
$resut = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result)){
    //set there status = 0 as offline
    $other_user_id = $row['user_id'];
    mysqli_query($con, "update tablename set status = 0 where user_id = '$other_user_id'");
}
//update your own timestamp
mysqli_query($con, "update tablename set timestamp = GETDATE() where user_id='$user_id'");

这将检查用户活动,或基本上检查js是否正在运行.如果关闭浏览器,则该用户的TS将不会更新,因此GETDATE()-时间戳将大于60.现在,其他将运行Web应用程序的用户也将运行相同的脚本,检查并更新所有用户的状态为每个条件. 当用户关闭其标签时,他仍然会在线至少1分钟.

This would check user activity, or basically check if that js is running or not. If browser was closed then TS for that user won't get updated hence GETDATE() - timestamp will be greater than 60. Now other users who would be running the web app would also run the same script, checking and updating all users status as per condition. When user closes his tab he would still be online for at least 1 minute.

我遇到了类似的问题,并在这里找到了解决方法关闭标签页上的PHP自动注销.

I had a similar problem and found solution here PHP auto logout on tab close.

这篇关于定时自动注销和浏览器关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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