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.