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

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>";

?>

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.

Each time a user visits a page, add the timestamp to the db. Then, in the code in which you are trying to retrieve all the active users, don't include users of which the current time minus the timestamp is greater than a certain value.

You can also do this in a more difficult way, by creating a Cron Job, which determines wether the user is still online by subtracting the timestamp from the current time.

You can also add a JavaScript widget that pings a keepalive message back to your server every minute or so and autokill sessions if you don't get one of these messages every few minutes or so, but that would increase the load on your server and could also cause session loss for users who experience flaky internet connections that drop and come back.

At the end of the day, there is no way to guarantee that you will receive notice when a user closes their browser. There are a number of strategies that you can use to enhance your ability to detect closed browsers, but all of them can be thwarted by clever users and even unusually configured browsers. The best practice is to have a session timeout with a reasonable value. If the user doesn't do anything for an hour, they have probably lost interest in your website and the utility of keeping their session open indefinitely for them to come back gradually diminishes to zero.