修改代码
此代码将引用我在此处编写的示例 >> jquery-idle with jquery-ui Dialog
使用的库:
嵌入式库示例:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://thorst.github.io/jquery-idletimer/prod//src/idle-timer.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
没有 jQuery 对话框:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://thorst.github.io/jquery-idletimer/prod//src/idle-timer.js"></script>
请记住,您可以使用您喜欢的任何对话方法来切换对话代码。我在对话框中包含了 jquery-ui,以使事情尽可能简单。这也不处理beforeunload 事件,因为您的代码中已经涵盖了该事件,但是我建议您在此处进一步阅读>> beforeunload * article
说明
HTML
这行代码用于存储倒数计时器的占位符。为简化起见,我也在计时器到期时使用它来显示“Session Expired”
<div id="sessionSecondsRemaining" style="font-size: 22px; font-weight: bolder;"></div>
这是一个使用 jQuery UI 的非常简单的模态对话框。您可以随意扩展或替换它。
<div id="dialog-confirm" title="Logout" style="display: none;">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;">Your session has expired.</span></p>
</div>
没有 jQuery 对话框
<div id="sessionSecondsRemaining" style="display:none;"></div>
CSS
这只是一个小技巧,因为灰色背景中的错误无法正确显示 jQuery UI 模式对话框(为什么尚未修复 - facepalm)
/* override jquery ui overlay style */
.ui-widget-overlay {
background-image: none !important; /* FF fix */
background: #000 url(images/new-ui-overlay.png) 50% 50% repeat-x;
}
没有 jQuery 对话框
Javascript
您可以在此部分配置 jquery-idletimer 的参数。
var
session = {
//Logout Settings
inactiveTimeout: 10000, //(ms) The time until we display a warning message
warningTimeout: 10000, //(ms) The time until we log them out
minWarning: 5000, //(ms) If they come back to page (on mobile), The minumum amount, before we just log them out
warningStart: null, //Date time the warning was started
warningTimer: null, //Timer running every second to countdown to logout
logout: function () { //Logout function once warningTimeout has expired
//window.location = settings.autologout.logouturl;
},
//Keepalive Settings
keepaliveTimer: null,
keepaliveUrl: "", // set the Keep Alive URL here (aka keepalive.php)
keepaliveInterval: 5000, //(ms) the interval to call said url
keepAlive: function () {
$.ajax({ url: session.keepaliveUrl });
}
}
;
要添加“keepalive.php”支持,只需设置 keepalive.php 所在位置的完整 URL(以及您希望传递的任何参数,因为您正在使用会话,所以您不需要任何参数)。
keepaliveUrl: "http://example.com/keepalive.php", // set the Keep Alive URL here (aka keepalive.php)
这一行,初始化并设置 #sessionSecondsRemaining div 中的值。
$('#sessionSecondsRemaining').html(Math.round((session.warningTimeout - diff) / 1000));
您可以在此部分放置控制对话框的代码,警告用户会话到期倒计时(通常 #sessionSecondsRemaining 会在此对话框中)
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Extend": function() {
clearTimeout(session.warningTimer);
$( this ).dialog( "close" );
},
Cancel: function() {
session.logout();
$( this ).dialog( "close" );
}
}
});
没有 jQuery 对话框
如果您注意到“扩展”,则终止警告计时器,并且 Cancel 会调用注销功能(也可在上面配置)
最后,这个块对于计时器倒计时到零的情况以及控制内部倒计时的显示非常重要#sessionSecondsRemaining
if (remaining >= 0) {
$('#sessionSecondsRemaining').html(remaining);
} else {
$( '#dialog-confirm' ).dialog( "close" );
clearInterval(session.warningTimer);
$( '#sessionSecondsRemaining' ).html('Session Expired');
session.logout();
}
在else 下,可能是您在上面的块中真正需要修改的唯一位置。在那里,我调用了session.logout() 函数(应该是清理对话框后的最后一行,但这只是一个演示)。这是您关闭对话框和/或将用户重定向到会话过期页面或显示消息的位置。如果停留在同一页面上,请确保clearInterval(session.warningTimer);。如果不是,那这条线就没有关系了。
没有 jQuery 对话框
if (remaining >= 0) {
$('#sessionSecondsRemaining').html(remaining);
} else {
clearInterval(session.warningTimer);
session.logout();
}
keepalive.php
if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); }
include 'db.php';
$maxtimeout = 15; // Seconds for max timeout before forcing session reset on other users.
mysql_query("UPDATE users SET status = 1 WHERE user_id = ".$_SESSION['user_id']."");
mysql_query("UPDATE users SET status = 0 WHERE user_id <> ".$_SESSION['user_id']." AND (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`timestamp_field`)) > " . $maxtimeout . "";
此任务应设置为在服务器端运行以清除任何杂散的数据库(如果您有大量活动,则不需要此脚本)
cron.php
include 'db.php';
// Set this for a longer timeout than in keepalive.php
$maxtimeout = 90; // Seconds for max timeout before forcing session reset on other users.
mysql_query("UPDATE users SET status = 0 WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`timestamp_field`)) > " . $maxtimeout . "";