有什么方法可以告诉Chrome在重新打开关闭的页面时不还原会话吗?也许是特殊的标题?
当我的员工登录我的公司应用程序时,它非常依赖于状态。如果它们在60分钟内处于非活动状态,我想注销它们并删除所有与他们正在做的事情有关的数据,但是问题是Chome具有此便捷功能,它将在您离开的地方重新打开页面,而不会出现任何问题一个东西。我的网站不知道他们已经关闭了浏览器两天,而setTimeout
不会再启动50分钟。
我还有另外两种怪异的解决方法,但是如果我可以告诉Chrome不要尝试挽救旧的会话,而是将所有打开的内容都视为第一次打开,我会更喜欢它。也许通过JavaScript禁用缓存?
编辑: 我正在使用IIS提供Angular 9静态html和javascript。
答案 0 :(得分:1)
因此,正如您提到的,您使用的是没有后端的静态网站。尽管您没有提及任何内容,但我假设您正在使用sessionStorage
或localStorage
来处理身份验证。如果是这样,您可以做的是在用户登录时设置计时器,并维护localStorage以跟踪空闲时间。
let obj_date = new Date();
let miliseconds = obj_date.getTime(); // Returns the number of miliseconds since 1970/01/01
localStorage.setItem("idle_time",miliseconds);
此后,请每隔10、20、30或60秒(根据您的选择)从setInterval()
之内调用以下函数,以检查该时间限制是否已过期。
function check_if_session_expired() {
let max_idle_minutes=60;
let miliseconds_now = obj_date.getTime();
let get_idle_time_in_miliseconds = localStorage.getItem("idle_time");
let one_minute_to_milisecond = 1000 * 60;
if ((Math.round(miliseconds_now / one_minute_to_milisecond) - Math.round(get_idle_time_in_miliseconds / one_minute_to_milisecond)) >= max_idle_minutes) {
console.log("expired");
//logout the user and clear sessionStorage/localStorage if you want
} else {
localStorage.setItem("idle_time",miliseconds_now);
}
}
您也可以使用cookie。
答案 1 :(得分:0)
您要做的是从服务器端销毁session
。检查下面用php
编写的代码。但是,如果您有了主意,则可以使用任何方法来实现。
<?php
session_start();
//Expire the session if user is inactive for 60 minutes or more.
$expireAfter = 60;
//Assign the current timestamp as the user's latest activity
$_SESSION['last_action'] = time();
//Check to see if our "last action" session variable has been set.
if(isset($_SESSION['last_action'])){
//Figure out how many seconds have passed since the user was last active.
$secondsInactive = time() - $_SESSION['last_action'];
//Convert our minutes into seconds.
$expireAfterSeconds = $expireAfter * 60;
//Check to see if they have been inactive for too long.
if($secondsInactive >= $expireAfterSeconds){
//User has been inactive for too long. Kill their session.
session_destroy();
unset($_SESSION);
header("Location: http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/example/login.php");
exit;
}
}
这只是一个简单的实现,您绝对可以对其进行扩展,使其以所需的方式工作。