简体   繁体   English

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

[英]How to track logout times session in php and save it in mysql as database

I want to ask about logout times session in PHP.我想问一下 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.我想让我的任务更容易,以便每月报告有多少客户/客户正在访问和使用我们的网站。

if you want to track user session, then you need to store data into their browser example (cookie).如果你想跟踪用户会话,那么你需要将数据存储到他们的浏览器示例(cookie)中。

Syntax for PHP PHP 的语法

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

Only the name parameter is required.只有名称参数是必需的。 All other parameters are optional.所有其他参数都是可选的。

you can use md5 or any hashing technique to mask your data.您可以使用 md5 或任何哈希技术来屏蔽您的数据。

But, if you don't want to store data into their browser, you can create temporary database for the current user session.但是,如果您不想将数据存储到他们的浏览器中,您可以为当前用户会话创建临时数据库。 Whenever the user creates any event (eg clicking button, or request a webpage), you can search for the database if he is already out.每当用户创建任何事件(例如单击按钮或请求网页)时,如果他已经不在,您就可以搜索数据库。

After removing data, you can delete the user's temporary data to save space.删除数据后,您可以删除用户的临时数据以节省空间。

Edit: Try using this instead编辑:尝试改用它

$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]'");

To get the hour/min/sec, first load the stored timestamp of the user in the database, then also get the current timestamp.要获取小时/分钟/秒,首先加载数据库中用户存储的时间戳,然后再获取当前时间戳。

//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
}

Since you are talking about client side.既然你在谈论客户端。 Add javascript.添加javascript。 Try this.试试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在有或没有 AJAX 的情况下使用 mysql 数据库和 php 会话登录/注销? - How to login/logout using mysql database and php session with or without AJAX? 如何将日期时间从MySQL保存到php的SESSION中? - How to save datetime from MySQL into SESSION in php? 在 php 和数据库中创建登录和注销会话 - create login and logout session in php and database PHP-使用会话,cookie或数据库登录/注销/过期 - PHP - Login/Logout/expire with Session,cookie or database 如何在没有数据库的情况下使用 php 或 MySQL 登录和注销博客? - how to make login and logout to blog with php without database or MySQL? 如何在不刷新php页面的情况下跟踪mysql数据库中的更改? - How to track to changes in mysql database without refreshing the php page? 如何通过按时间顺序在php中跟踪mysql数据库表条目 - How to keep track of mysql database table entries by chronological insertion in php 如何在MySQL中为PHP在数据库中保存图像路径 - how to save image path in the database in mysql for php 如何将特殊字符保存到 PHP 中的 Mysql 数据库 - How to Save Special Character to Mysql Database in PHP 不活动时如何删除mysql数据库中记录的会话并自动注销用户 - How can I delete recorded session in mysql database and automatically logout user after inactivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM