1

I want to ask about Logout tracking when session is closed, without explicit logout button action by the user.

Following is the code used during Login action, which will create a new row in track_log_user table with value username, login_date and login_hour.

<?php 
    require_once ('connections.php');
    // the login validation page 

    $the_username = $_POST['the_username'];
    $the_password = $_POST['the_password'];

    $login_date = date("Y-m-d");
    $login_time = date("H:i:s");

    $query = mysqli_query($conn,"SELECT * 
                                 FROM tbl_user 
                                 WHERE usr = '$the_username' AND 
                                       pwd = '$the_password'");
    $result = mysqli_num_rows($query);


    if ($result > 1) {
        $fetch = mysqli_fetch_assoc($query);
        $_SESSION['the_username'] = $fetch['usr'];
        $_SESSION['the_password'] = $fetch['pwd'];
        mysqli_query($conn,"INSERT INTO track_log_user(`id`,`username`,`login_date`,`login_time`,`logout_date`,`logout_time`) VALUES (NULL,`$_SESSION[the_username]`,'$login_date','$login_time','','')");
        header("location:my_menu.php");
    }
    else {
        echo "Your Login Is Incorrect";
        header("location:index.php");
    }

  ?>

Following is the code, that will work Logout Button action by the user. It will fill the login tracking time before with the logout_date and logout_time value.

<?php
require_once ('connections.php');
// this is logout page when user click button logout in system page

session_start();
$time = date("H:i:s");
$date = date("Y-m-d");
mysqli_query($conn, "UPDATE track_log_user SET logout_date = '$date' AND logout_time = '$time' WHERE username = '$_SESSION[the_username]'");
$_SESSION = NULL;
$_SESSION = [];
session_unset();
session_destroy();
header("location:index.php");
?>

My question is how can I track if the client is idle or force closed window/tab so that their session ends automatically and logout??

How will my user login and logout date-time tracking system work ? I am making a monthly report how many customers/clients visiting and using our website.

Please give me a suggestion for the code

Thank you.

Yan Erick
  • 17
  • 1
  • 6
  • Possible duplicate of [Trying to detect browser close event](https://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event) – JBH Sep 01 '18 at 07:22
  • Yan, welcome to Stack Overflow. We've been asked before about how to detect browser closure, which is why I marked your question as a duplicate. Detecting idle time isn't hard. Everytime a user does something or changes pages, you log a new "last_action_time" to their session. You test automatic logout against that time and use the browser close event with it, too. – JBH Sep 01 '18 at 07:25
  • ok thank u for a refferal link but i think it not solve my problem, because my problem is how about when my clients login and suddenly logged out because unresponsive browser or crash or carelessness from them that will make my tracking login and logout time system will not working properly ( cant record when that user logged out, just get the record time of when they logged in) – Yan Erick Sep 01 '18 at 08:41
  • 1
    Yan, there are some things you can't solve. If the power goes out, the only way you'll know the user logged out is by a timeout value (idle time). However, the linked question explains how to detect when someone closes a tab or browser so you can log them out. It's most of what you need (that and a little AJAX). – JBH Sep 01 '18 at 08:43
  • okay thank u very much JBH. – Yan Erick Sep 01 '18 at 08:55
  • Possible duplicate of [Destroy or unset session when user close the browser without clicking on logout](https://stackoverflow.com/questions/2839988/destroy-or-unset-session-when-user-close-the-browser-without-clicking-on-logout) – Progman Sep 01 '18 at 08:57

0 Answers0