php - 如何在php中跟踪注销时间会话并将其保存在mysql中作为数据库
问题描述
我想问一下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");
?>
我的问题是如果客户端空闲或浏览器错误或笔记本电脑崩溃或强制关闭窗口/选项卡并且他们的会话自动结束并注销?
我的跟踪用户登录和注销日期时间后端系统将如何工作?
我想让我的任务更轻松,每月报告有多少客户/客户正在访问和使用我们的网站。
解决方案
如果您想跟踪用户会话,那么您需要将数据存储到他们的浏览器示例(cookie)中。
PHP 的语法
setcookie(name, value, expire, path, domain, secure, httponly);
只有名称参数是必需的。所有其他参数都是可选的。
您可以使用 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>
推荐阅读
- html - iOS Chrome 自动填充在我的网站上不起作用
- reactjs - 带有路由器链接的 Material-ui 排版始终带下划线
- react-native - 如何在反应原生的功能组件中使用 forwardRef?
- typescript - 如何修复“属性没有初始化程序并且未在构造函数中明确分配”错误?
- substrate - 底物创世区块不匹配
- python - SQLAlchemy:使用 in_ 运算符一次删除多条记录
- python - 从 gensim.models.keyedvectors.Word2VecKeyedVectors 类型的模型传递到 gensim.models.word2vec.Word2Vec 类型的模型
- javascript - 如何让线条在 d3.js 多折线图上呈现?
- python - 如何使用其他列和字符串格式在熊猫数据框中生成列
- sum - 图表。在堆积条形图中显示值的总和