早上好, 当用户连接但不活动 30 分钟将被 Plesk 服务器关闭时,我正在寻找标识符 (30 分钟不活动是关闭会话前我的服务器设置)
我有一个用 PHP 开发的应用程序在 plesk 下托管和管理。 因为 plesk 我在关闭会话之前确定不活动的时间。 当用户打开页面超过 30 分钟没有任何活动时,他的会话将自动关闭,但不会发出警告或消息。 所以为了防止他相信应用程序不再工作,我希望能够向他显示一条消息,表明会话已过期,但我无法这样做
谢谢你的帮助。
在每分钟调用的函数中,我进行以下测试:
if(session_status() === PHP_SESSION_DISABLED || session_status() === PHP_SESSION_NONE ){
respAjax::errorJSON(array('message' => ($usrActif->lang == 'FR' || $usrActif->lang == '' ? 'SESSION EXPIREE' : 'SESSION EXPIRED')));
}
您可以在用户登录时启动计时器
let inactivityTimer = setTimeout(function() {
// User has been inactive for 30 minutes, display message
alert('Your session has expired due to inactivity.');
}, 30 * 60 * 1000); // 30 minutes in milliseconds
然后您可以在任何活动发生时重置计时器
// Reset the timer when the user performs an action
function resetTimer() {
clearTimeout(inactivityTimer);
inactivityTimer = setTimeout(function() {
alert('Your session has expired due to inactivity.');
}, 30 * 60 * 1000);
}
// Bind resetTimer to various events that indicate user activity
document.addEventListener('mousemove', resetTimer);
document.addEventListener('keydown', resetTimer);
document.addEventListener('scroll', resetTimer);
// etc.
您还可以在用户注销时清除计时器
// Clear the timer when the user logs out
function logout() {
clearTimeout(inactivityTimer);
// Do any other logout logic
}
注意: 这与你的 php 无关,这都是由 javascript 完成的
此外,这可能对某人有帮助:
另一件事,如果您不希望会话一整天都过期或直到浏览器关闭,那么您可以每 5-10 分钟(相应地)通过 ajax 向服务器进行一次简单的 jquery 调用,它永远不会过期。如果需要,您还可以使用此 ajax 请求在客户端更新值。
这里是例子:
function keepSessionAlive() {
$.ajax({
url: "keepSessionAlive.php",
success: function() {
// Session is still alive
},
error: function() {
// Session has expired or there was an error
}
});
}
setInterval(keepSessionAlive, 5 * 60 * 1000); // 5 minutes in milliseconds