设计经典ASP应用程序以动态检测会话到期

时间:2022-06-20 01:38:57

I've got a Classic ASP application that relies on session; if the user leaves a screen idle and then runs a form post or other operation, I'd like to know whether the session has expired.

我有一个依赖会话的经典ASP应用程序;如果用户使屏幕空闲然后运行表单帖子或其他操作,我想知道会话是否已过期。

Currently I'm checking session in each page to see if it's timed out, but is there a better, dynamic, JavaScripty approach that will do what banks do and time out sessions with a notification and redirect to login?

目前我正在检查每个页面中的会话以查看它是否已超时,但是是否有更好的,动态的JavaScripty方法可以执行银行的操作,并通过通知超时并重定向到登录?

1 个解决方案

#1


During your page's onload event, start a timer, and then redirect the page after N seconds.

在页面的onload事件期间,启动计时器,然后在N秒后重定向页面。

  • For the timer, use the window.setTimeout function.
  • 对于计时器,请使用window.setTimeout函数。

  • For the redirect, set the value of window.location.
  • 对于重定向,请设置window.location的值。

Reusable Example:

<head>
<script type="text/javascript">
<!--
 function redirect(url) {
   window.location = url;
 }
 function beginSessionTimer() {
   // 30000ms = 30s
   window.setTimeout(redirect, 30000, 
                     "http://www.yoursite.com/login.asp?session=clear");
 }
//-->
</script>
</head>
<body onload='beginSessionTimer();'>
</body>

Quick-n-dirty Example w/ an inline function:

具有内联功能的Quick-n-dirty示例:

<body onload='window.setTimeout(function(){
window.location="http://www.yoursite.com/login.asp?session=clear";}, 
30000);'>

Note that if your page performs any AJAX calls, that keeps the session alive, so you'll want to reset the timer using the clearTimeout method (combined w/ a new call to setTimeout). For details on clearTimeout, click here for excellent documentation from Mozilla.)

请注意,如果您的页面执行任何AJAX调用,这会使会话保持活动状态,那么您将需要使用clearTimeout方法重置计时器(与对setTimeout的新调用相结合)。有关clearTimeout的详细信息,请单击此处获取Mozilla的优秀文档。)

#1


During your page's onload event, start a timer, and then redirect the page after N seconds.

在页面的onload事件期间,启动计时器,然后在N秒后重定向页面。

  • For the timer, use the window.setTimeout function.
  • 对于计时器,请使用window.setTimeout函数。

  • For the redirect, set the value of window.location.
  • 对于重定向,请设置window.location的值。

Reusable Example:

<head>
<script type="text/javascript">
<!--
 function redirect(url) {
   window.location = url;
 }
 function beginSessionTimer() {
   // 30000ms = 30s
   window.setTimeout(redirect, 30000, 
                     "http://www.yoursite.com/login.asp?session=clear");
 }
//-->
</script>
</head>
<body onload='beginSessionTimer();'>
</body>

Quick-n-dirty Example w/ an inline function:

具有内联功能的Quick-n-dirty示例:

<body onload='window.setTimeout(function(){
window.location="http://www.yoursite.com/login.asp?session=clear";}, 
30000);'>

Note that if your page performs any AJAX calls, that keeps the session alive, so you'll want to reset the timer using the clearTimeout method (combined w/ a new call to setTimeout). For details on clearTimeout, click here for excellent documentation from Mozilla.)

请注意,如果您的页面执行任何AJAX调用,这会使会话保持活动状态,那么您将需要使用clearTimeout方法重置计时器(与对setTimeout的新调用相结合)。有关clearTimeout的详细信息,请单击此处获取Mozilla的优秀文档。)