doudi1979 2016-07-22 19:50
浏览 71
已采纳

注销时会话结束,但数据库中没有更新..想法?

Alright so when the user logs in, a session is started and Statusin the database is updated to '1'. When the user logs out, the session is ended and Statusin the database is updated to '0'.

The issue is, the session ends when a user closes his web browser, which is perfectly okay, but the database Statusis not updated which shows the user still online.

Suggestions?

EDIT : This is the new "online-staff" section.

<?php
include 'connection.php';

$query = "SELECT * FROM Admins WHERE AdminLevel>='1' AND Status='1'";
$result = mysqli_query($connect,$query);


echo "
  <table class='table-fill'>
    <thead>
      <tr>
        <th class='text-left' style='padding-left: 12px; padding-right: 12px;';></th>
      </tr>
    </thead>
    <tbody class='table-hover'>";

    if (($result->num_rows) >= 1) {
        $results = mysqli_fetch_array($result);
        $currentTime = NOW();
        while ($results) {
            $visitTime = $results['LastVisited'];
            $interval = ($visitTime->diff($currentTime))->minutes;
            if ($interval<=15) {
                if ($results['AdminLevel'] == 3){
                    $rank = 'In-Training/Intern';
                }elseif ($results['AdminLevel'] == 6){
                    $rank = 'Community Moderator';
                }elseif ($results['AdminLevel'] == 9){
                    $rank = 'Senior Moderator';
                }elseif ($results['AdminLevel'] == 12){
                    $rank = 'Supervisor';
                }elseif ($results['AdminLevel'] == 15){
                    $rank = 'Administrator';
                }elseif ($results['AdminLevel'] == 18){
                    $rank = 'Senior Administrator';
                }elseif ($results['AdminLevel'] == 21){
                    $rank = 'Staff Advisor';
                }elseif ($results['AdminLevel'] == 24){
                    $rank = 'Engineer';
                }elseif ($results['AdminLevel'] == 27){
                    $rank = 'Vice Chairman';
                }elseif ($results['AdminLevel'] == 30){
                    $rank = 'Chairman';
                }elseif ($results['AdminLevel'] == 33){
                    $rank = 'Website Engineer';}
                echo "<tr>";
                echo "<td>" . $results['Username'] . " - " . $rank . "</td>";}
            }

    }else{
        echo "<tr>";
        echo "<td>" . 'There are no staff members online.' . "</td>";
    }

    echo " </tbody>
  </table>";

?>

  • 写回答

3条回答 默认 最新

  • doubo9799 2016-07-22 20:00
    关注

    Basicly you have two options:

    1. Use a javascript that triggers on unload event and calls a ajax to update your db. This is not a 100% realiable option, because if the javascript is turned off, or can't run for some reason, your ajax will not be executed.
    2. Your best approach is store the time of the last action of the user. If longer than X minutes ago, you can update your db.

    Edit:

    About method 2:

    The logic is something like this: When you start any session with an user logged in, you must update your db with the current time (you can use mysql NOW() if you are using timestamp) and check if the user's status is already 1.

    After that, when you need to check if the user status is 0 or 1, you run a check of the last activity of all users with status 1. If this time is more than X minutes you can consider this user offline and update the status to 0.

    This is just one of many ways you can do that. If you face any trouble while applying this, let me know in the comments and I will update my answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab