如何在 JavaScript 中每次检查用户会话是否过期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15063795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Check every time if user session is expired in JavaScript
提问by Shailesh
Check every time if Session has expired and if yes then redirect to home page by using JavaScript such as facebook do if I press ctrl+shift+delthen it shows login window. I am using .aspx web pages and using WCF services
每次检查会话是否已过期,如果是,则使用 JavaScript 重定向到主页,例如 facebook do 如果我按 ctrl+ shift+del然后它会显示登录窗口。我正在使用 .aspx 网页并使用 WCF 服务
回答by Mehdi Bugnard
I do not know if this is the best solution, but here's a solution that will test every 500 milliseconds if the user session has expired
我不知道这是否是最好的解决方案,但这里有一个解决方案,如果用户会话已过期,它将每 500 毫秒进行一次测试
Checker permanently a session variable with "setInterval" function every 500 milliseconds :
每 500 毫秒检查一次带有“setInterval”函数的会话变量:
function CheckSession() {
var session = '<%=Session["username"] != null%>';
//session = '<%=Session["username"]%>';
if (session == false) {
alert("Your Session has expired");
window.location = "login.aspx";
}
}
setInterval(CheckSession(),500);
见这里:http: //www.daniweb.com/web-development/javascript-dhtml-ajax/threads/362898/check-for-session-timeout-in-javascript-and-redirect-to-login-page#
回答by Calipso
You can basically check on page load if any of your session variables which is supposed to have a value is null or not. If the session variable is null then redirect page.
您基本上可以检查页面加载是否有任何应该具有值的会话变量是否为空。如果会话变量为空,则重定向页面。
回答by Pannan Mp
you could write a php file to check whether session has expired , if expired then header must be given argument as Location:homepage
this file must be included in each of your page by adding the
"include'filename.php'" or "require once" function
您可以编写一个 php 文件来检查会话是否已过期,如果已过期,则必须将标头作为 Location:homepage 参数
通过添加
“include'filename.php'”或“require once”包含在您的每个页面中“ 功能