如何在php中跟踪会话的注销时间,并将其作为数据库保存在mysql中

uoifb46i  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(282)

我想问一下php中的注销时间会话。
用户登录系统时的代码:

<?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");
    }

  ?>

如果用户单击“注销”按钮,则返回代码:

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

我的问题是,如果客户端空闲,浏览器出错,笔记本电脑崩溃,或者强制关闭窗口/选项卡,他们的会话会自动结束并注销?
我的跟踪用户登录和注销日期时间后端系统将如何工作??
我想让我的任务更容易,使每月有多少客户/客户访问和使用我们的网站报告。

b91juud3

b91juud31#

如果要跟踪用户会话,则需要将数据存储到他们的浏览器示例(cookie)中。
php语法

setcookie(name, value, expire, path, domain, secure, httponly);

只需要name参数。所有其他参数都是可选的。
您可以使用md5或任何哈希技术来屏蔽数据。
但是,如果不想将数据存储到他们的浏览器中,可以为当前用户会话创建临时数据库。每当用户创建任何事件(例如,单击按钮或请求网页)时,如果他已外出,则可以搜索数据库。
删除数据后,可以删除用户的临时数据以节省空间。
编辑:尝试改用这个

$datetime = date("Y-m-d H:i:s"); 
$timestamp = strtotime($datetime); //in milliseconds
mysqli_query($conn,"UPDATE users SET logout_timestamp = '$timestamp ' WHERE username = '$_SESSION[the_username]'");

要获取小时/分钟/秒,首先将存储的用户时间戳加载到数据库中,然后还要获取当前时间戳。

//set maximum session (global variable), example 30 minutes
$_MAXIMUM_SESSION_IN_SEC = 60*30;

$timestamp_difference = $new_timestamp - $old_timestamp
if($timestamp_difference/1000 > $_MAXIMUM_SESSION_IN_SEC)
{
    //logout here
}
else 
{
    //remaining time for display
    $seconds= floor((timestamp_difference % (1000 * 60)) / 1000);
    $minutes= floor((timestamp_difference % (1000 * 60 * 60)) / (1000 * 60));
    $hour = floor((timestamp_difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    //echo here
}

既然你说的是客户。添加javascript。试试这个。

<script>
// fetch data from sql
var oldTimestamp = <?php echo $timestamp_from_sql; ?> ;

// Update the every 1 second
var x = setInterval(function() {

  // Get current timestamp
  var currentTimestamp = Date.now();

  // Find timestamp difference
  var timestamp_difference = currentTimestamp - oldTimestamp;

  // Time calculations for hours, minutes and seconds
  var hours = Math.floor((timestamp_difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((timestamp_difference % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((timestamp_difference % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = hours + "h "
  + minutes + "m " + seconds + "s ";

  // If above the session limit, logout the user
  if (distance > maximumSessionInSec) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "SESSION EXPIRED";
    //add other code here to reload the page
    //or add ajax code to update files in the server
  }
}, 1000);
</script>

相关问题