0

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

?>

Austin
  • 41
  • 6
  • 1
    unless you have some 100% reliable way of telling when the user closes their browser, you can't detect when that happens, and your only choice is to expire server-side session data after some delay period. – Marc B Jul 22 '16 at 19:52

3 Answers3

2

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.

Martijn
  • 514
  • 6
  • 12
  • Can you help me set the time checker/inactivity checker up? – Austin Jul 22 '16 at 20:15
  • This post will probably help you out: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php. You can generate the date using the date() function: http://www.w3schools.com/Php/php_date.asp. – Martijn Jul 22 '16 at 20:22
2

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.

Clyff
  • 4,046
  • 2
  • 17
  • 32
  • Will the (longer than) have to be ran via a trigger in MYSQL? – Austin Jul 22 '16 at 20:03
  • And would I want the field to be a "TimeStamp" or "Time" ? – Austin Jul 22 '16 at 20:04
  • Not necessarily. You can do that while the information is needed. Use either timestamp or datetime for that. – Clyff Jul 22 '16 at 20:07
  • Will you help me detect/setup an inactivity checker? – Austin Jul 22 '16 at 20:09
  • And where I log the user in, I should just be able to use NOW() with timestamp, correct? – Austin Jul 22 '16 at 20:10
  • Updated the answer with the general idea of what to do. – Clyff Jul 22 '16 at 20:21
  • I have "LastVisited" as a timestamp. First question: Do I need to add an updater on every page? Second question: How do I insert a script that automatically runs without the user navigating to the page? Example: If the user isn't on the site, how does the checker for inactivity know to check? – Austin Jul 22 '16 at 20:32
1

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.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40