显示在jQuery的超时警告 [英] Displaying timeout warning within jQuery

查看:116
本文介绍了显示在jQuery的超时警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理超时从PHP Web应用程序中,例如:

  • 在每个页面上目前有一个超时检查在PHP中,它会针对用户的最后一个页面加载当前时间。如果两者间的差超过15分钟,在用户的会话被破坏,新的会话被建立,当前页的位置上载置会话和用户被重定向到登录页面。
  • 在每个页面头部有一个<元> 标签,15分钟,5秒钟后刷新页面(这样,呼吁超时检查)

我现在目前正在研究将更好地控制了用户,例如,如果他们碰巧是工作在页面上进行的很长一段时间,并期待在允许一个弹出窗口出现前大约2分钟时间到。弹出窗口将不得不选择,要么继续会话(使一个AJAX调用刷新上次活动会话),或注销。如果弹出被忽略(如果用户是在不同的页面,例如),用户将被注销。

据<一href="http://stackoverflow.com/questions/3252743/using-javascript-to-override-or-disable-meta-refresh-tag">Using Javascript来重写或禁用元刷新标记,我将无法重置&LT;元&GT; 标记,这可能会意味着我将有去除标签。 <一href="http://stackoverflow.com/questions/11538803/php-javascript-session-timeout-with-warning">PHP/Javascript会话超时与警告的唯一答案建议使用一个JavaScript调用重定向到登录页面,但是这可以通过禁用JavaScript的规避。

我在想什么这样做是围绕&LT;元&GT; 重定向标记与&LT; NOSCRIPT&GT; (容许因为我使用HTML5),这样,即使用户不使用JavaScript,它们仍然会超时。这也将删除&LT;元&GT; 标记引发了用户是否决定继续他们的会话

请问这种做法是否合理?我失去了一些东西?是否有另一种方法,这将使更有意义?


我目前的code

 &LT; PHP
require_once(包括/ session.php文件);
require_once(包括/ sessioncheck.php);
?&GT;
&LT; HTML&GT;
    &LT; HEAD&GT;
         &LT; META HTTP-当量=刷新内容=&LT; = TIMEOUT_MIN * 60 GT;? /&GT;
         &LT;! - 额外的标记 - &GT;
    &LT; /头&GT;
    &LT;身体GT;
        &LT;! - 内容 - &GT;
    &LT; /身体GT;
&LT; / HTML&GT;
 

解决方案

我最终什么做什么是离开我的sessioncheck.php code并删除&LT;元&GT; 标记。然后我说下面的封闭体标记之前:

 &LT; D​​IV ID =超时预警&GT;&LT; / DIV&GT;
&所述;股利的id =超时警告文本&GT;您的会话设定为在少于一分钟到期
    由于不活动。点击&LT; A HREF =JavaScript的:无效(0)ID =暂停重启&GT;
    此处&lt; / A&GT;让您的会议活着&LT; / DIV&GT;

&LT;脚本类型=文/ JavaScript的&GT;
    VAR timeoutWarning;
    VAR超时;
    $(文件)。就绪(函数(){
        $(#暂停重启)。点击(函数(){
            clearTimeout(超时);
            超时= setTimeout的(功能(){
                document.location.reload(假);
            },1801000);
            timeoutWarning = setTimeout的(功能(){
                $(#超时预警)fadeTo(2000年,0.5)。
                $(#超时警告文本)淡入(2000);
            },174万);
            $(#超时预警)隐藏();
            $(#超时预警文本)隐藏();
            返回false;
        });

        timeoutWarning = setTimeout的(功能(){
            $(#超时预警)fadeTo(2000年,0.5)。
            $(#超时警告文本)淡入(2000);
            clearTimeout(softTimeout);
        },174万);
        超时= setTimeout的(功能(){
            document.location.reload(假);
        },1801000);
    });
    &LT; / SCRIPT&GT;
 

的1740000和1801000号是基于从PHP常量,这恰好是基于一个半小时的超时时间。

I currently handle timing out from a PHP web application as such:

  • Each page currently has a timeout check in PHP, which checks the current time against the last page load by the user. If the difference between the two exceeds 15 minutes, the user's session is destroyed, a new session is created, the current page location is put onto the session and the user is redirected to the login page.
  • Each page head has a <meta> tag which refreshes the page after 15 minutes and 5 seconds (thus "calling" the timeout check)

I am now currently looking at adding greater control for a user, for example, if they happen to be working on a page for a long period of time, and am looking at allowing for a popup to appear roughly two minutes before a timeout. The popup would then have options to either continue the session (make an AJAX call to refresh the last-activity in the session) or to log out. If the popup is ignored (if the user is on a different page, for example), the user is logged out.

According to Using Javascript to override or disable meta refresh tag, I won't be able to reset the <meta> tag, which will likely mean that I'll have to remove the tag. PHP/Javascript Session Timeout with warning's sole answer suggests to use a JavaScript call redirect to the login page, however this can be circumvented by disabling JavaScript.

What I'm thinking about doing is surrounding the <meta> redirect tag with <noscript> (allowable since I'm using HTML5), so that even if the user doesn't use JavaScript, they will still be timed out. This will also remove the <meta> tag from triggering whether or not the user decides to continue their session.

Would this approach make sense? Am I missing something? Is there another approach that would make better sense?


My current code

<?php
require_once("include/session.php");
require_once("include/sessioncheck.php");
?>
<html>
    <head>
         <meta http-equiv="refresh" content="<?= TIMEOUT_MIN * 60 ?>" />
         <!-- additional tags -->
    </head>
    <body>
        <!-- content -->
    </body>
</html>

解决方案

What I ended up doing was leaving my sessioncheck.php code in and removing the <meta> tag. I then added the following before the closing body tag:

<div id="timeout-warning"></div>
<div id="timeout-warning-text">Your session is set to expire in less than one minute
    due to inactivity. Click <a href="javascript:void(0)" id="timeout-restart">
    here</a> to keep your session alive.</div>

<script type="text/javascript">
    var timeoutWarning;
    var timeout;
    $(document).ready(function() {
        $("#timeout-restart").click(function() {
            clearTimeout(timeout);
            timeout = setTimeout(function() {
                document.location.reload(false);
            }, 1801000);
            timeoutWarning = setTimeout(function() {
                $("#timeout-warning").fadeTo(2000, 0.5);
                $("#timeout-warning-text").fadeIn(2000);
            }, 1740000);
            $("#timeout-warning").hide();
            $("#timeout-warning-text").hide();
            return false;
        });

        timeoutWarning = setTimeout(function() {
            $("#timeout-warning").fadeTo(2000, 0.5);
            $("#timeout-warning-text").fadeIn(2000);
            clearTimeout(softTimeout);
        }, 1740000);
        timeout = setTimeout(function() {
            document.location.reload(false);
        }, 1801000);
    });
    </script>

The 1740000 and 1801000 numbers are based from a PHP const, which happens to be based on a timeout of half an hour.

这篇关于显示在jQuery的超时警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆