有人知道我可以在哪里阅读教程吗,或者知道如何创建内置警告的基于Javascript的会话超时,以及可选的以下功能吗:
- 用户活动重置计时器
- 与数据库交互(最后一次出现在等)
- 如果处于非活动状态,它将注销用户(通过重定向到logout.php第页)
- 在用户注销之前,它将显示一条弹出消息,询问如果他们想继续
不幸的是,我对Javascript了解不多。
我不知道你的网站是怎么做的,但如果做得好,你应该有一个登录会话和某种后端控制系统,如果前一个操作是在X分钟/小时前进行的,它会拒绝任何操作,并自动使用户过期。如果你想实现一些客户端代码,你应该有一个javascript定时器,在到期时间即将结束时提醒用户,你也可以在到期时间到来后将用户重定向到主页或登录页面。这样,所有的安全功能都在后端,javascript只能作为显示行为的显示度量。
更新:
setInterval(function(){alert("Hey, your session is ending")},360000);
setInterval(function(){
redirect();
},720000);
function redirect(){
document.location = "../logout.php"
}
更新2:
setInterval(function(){
logout();
},600000);
function logout(){
if(confirm('Logout?'))
redirect();
else
alert('OK! keeping you logged in')
}
function redirect(){
document.location = "../logout.php"
}
每个包含此代码的页面都会在10分钟后询问用户是否要注销。这意味着您的会话不能自行过期,您必须将控制权留给用户
会话在5分钟后注销
<script type="text/javascript">
var interval;
$(document).on('mousemove', function () {
clearInterval(interval);
var coutdown = 5 * 60, $timer = $('.timer'); // After 5 minutes session expired (mouse button click code)
$timer.text(coutdown);
interval = setInterval(function () {
$timer.text(--coutdown);
if (coutdown === 0) {
alert("Session expired. User successfully logged out.");
window.location = "UserLogin.php";
}
}, 1000);
}).mousemove();
var interval;
$(document).on('keydown', function () {
clearInterval(interval);
var coutdown =5 * 60, $timer = $('.timer'); // After 5 minutes session expired (keyboard button press code)
$timer.text(coutdown);
interval = setInterval(function () {
$timer.text(--coutdown);
if (coutdown === 0) {
alert("Session expired User successfully logout.");
window.location = "UserLogin.php";
}
}, 1000);
}).mousemove();
<script>
<html>
<div class="timer">
Time of session display on page
</div>
</html>