[英]PHP/Javascript Session Timeout with warning
有誰知道我在哪里可以閱讀教程,或者知道如何創建內置了警告的基於Javascript的會話超時以及以下可選功能:
不幸的是,我對Java不太了解。
我不知道您的網站是如何完成的,但是如果操作正確,您應該擁有一個登錄會話和某種后端控制系統,如果先前的操作是在X分鍾/小時前完成的,則該操作將拒絕任何操作並自動過期用戶。 如果要實現一些客戶端代碼,則應該有一個JavaScript計時器,用於在到期時間即將完成時提醒用戶,還可以在達到到期時間后將用戶重定向到首頁或登錄頁面。 這樣,所有安全功能都位於后端,而javascript僅用作顯示行為的一種顯示方式。
更新:
setInterval(function(){alert("Hey, your session is ending")},360000);
setInterval(function(){
redirect();
},720000);
function redirect(){
document.location = "../logout.php"
}
UPDATE2:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.