php - 如何在php中跟踪注销时间 session 并将其作为数据库保存在mysql中

标签 php mysql mysqli timestamp session-timeout

我想问一下 PHP 中的注销时间 session 。

用户登录系统时的代码:

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

我的问题是,如果客户端空闲或浏览器错误或笔记本电脑崩溃或强制关闭窗口/选项卡并且其 session 自动结束并注销怎么办?

我的跟踪用户登录和注销日期时间后端系统将如何工作?

我希望更轻松地制作月度报告,了解有多少客户/客户访问和使用我们的网站。

最佳答案

如果您想跟踪用户 session ,那么您需要将数据存储到他们的浏览器示例(cookie)中。

PHP 语法

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

仅需要名称参数。所有其他参数都是可选的。

您可以使用 md5 或任何哈希技术来屏蔽您的数据。

但是,如果您不想将数据存储到浏览器中,您可以为当前用户 session 创建临时数据库。每当用户创建任何事件(例如单击按钮或请求网页)时,如果他已经外出,您可以搜索数据库。

删除数据后,您可以删除用户的临时数据以节省空间。

编辑: 尝试使用这个代替

$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>

关于php - 如何在php中跟踪注销时间 session 并将其作为数据库保存在mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52124796/

相关文章:

mysql - 如何从 MySql 中的存储过程返回表?

mysql - 如何构成子查询

php - 我正在尝试使用 php 从我的域服务器从 mysql 获取数据到 android 应用程序

php - mysqli::mysqli(): (HY000/2002): 无法通过套接字 'MySQL' 连接到本地 MySQL 服务器 (2)

php - 在 HTML 中搜索 2 个短语(忽略所有标签)并删除其他所有内容

php - 用于管理显示哪些内容的模板系统?

php - 想要 'sandbox'用户表单提交HTML

MySQL 未经身份验证的用户

php - 在 HTML Select Tag 中使用数据库输出和 PHP

PHP:MY​​SQL 查询 排序依据