I want to ask about logout times session in PHP.
The code if the user logs into the system:
<?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");
}
?>
The code if user clicks logout button:
<?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");
?>
And my question is how if the client is idle or browser error or laptop crash or force close window/tab and their session is ended automatically and logout?
How my tracking user login and logout date-time backend system will working??
I want to make my task easier for making a monthly report for how many customers/clients are visiting and using our website.