繁体   English   中英

防止与我的Web应用程序多次连接

[英]Prevent multiple connexion to my web application

我想防止用户在同一浏览器(窗口或标签)中多次连接到我的Web企业应用程序。

我如何使用Javascript做到这一点?

您可以创建一个会话变量(日期和时间)并将其存储在localStorage中。 在window.load上检查它,如果存在,则关闭窗口,否则阻止进一步加载。

编辑:

我创建了一个完整的解决方案,也许以后会发现它的一些用途:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>test</title>
    <script>
        (function () {
            var interval = 10000; //can be modified to be faster, now it's 10 seconds
            // localStorage is available everywhere
            // sessionStorage is available only in current page / tab, 
            // so we can allow page refresh
            if (localStorage.getItem("catch") && !sessionStorage.getItem("catch")) {
                var date = new Date(parseInt(localStorage.getItem("catch"), 10));
                if (Date.now() - date < interval) {
                    //prevent further loading, hide things etc.
                    document.write('catch');
                }
            } else {
                setInterval(function () {
                    var date = Date.now();
                    localStorage.setItem("catch", date);
                    sessionStorage.setItem("catch", date);
                }, interval);
            }
        })();
    </script>
</head>
<body>
</body>
</html>

我可以想象使用Cookie或localStorage的解决方案:

  1. 页面加载:
    • 检查X是否作为cookie或localStorage存在。 如果存在,则显示错误并重定向到错误页面。
    • 如果不存在X ,请继续。
  2. 在页面onload( window.unloadwindow.onbeforeunload事件)上,删除X

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM