我想询问有关PHP中注销时间会话的Web开发人员
用户登录系统时的代码
<?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");
?>
我的问题是,如果客户端空闲,浏览器错误,笔记本电脑崩溃或强制关闭窗口/选项卡,并且会话自动结束并注销,该怎么办?
我的跟踪用户登录和注销日期时间后端系统将如何工作?
为了使我的工作更轻松,我每月制作一个报告,以了解有多少客户/客户访问和使用我们的网站。
请给我一个建议的密码
谢谢。
答案 0 :(得分:0)
如果要跟踪用户会话,则需要将数据存储到他们的浏览器示例(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>