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条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏