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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?