2009-02-21 120 views
6

假設您正在執行銀行應用程序。如果用戶登錄到您的網站,如何檢測他們的不活動並要求他們在一段時間內保持不活動狀態時退出?此處不活動意味着它們要麼切換到其他選項卡,要麼不切換到瀏覽器應用程序。如果用戶在特定時間段內處於非活動狀態,則強制註銷用戶

我想我可以通過註冊每個鼠標移動或鍵盤移動來做到這一點,當用戶在我的應用程序的每一頁上進行操作。但是代碼會非常難看,很難維護。有沒有其他更優雅的方式來做到這一點?

+0

你是什麼意思退出? – Gumbo 2009-02-21 13:14:47

+0

我改變了問題,使它更清晰。 – Graviton 2009-02-21 13:21:44

回答

6

如果用戶定期從服務器請求新的頁面/數據,那麼在PHP中調整會話超時應該適用於此(假設您正在使用PHP會話)。

如果擔心他們可能坐在一頁上很長時間沒有去服務器(例如填寫一個長表格),並且您想區分這一點和用戶,只需切換到另一個窗口,你可以做一些事情,比如使用javascript來每隔五分鐘使用XMLHTTPRequest請求一些數據以保持會話的活躍。你可以在javascript中使用window.focus和window.onblur事件來停止和重啓這個機制(我認爲IE有一些區別,有一個很好的解釋here)。

0

通常會話生存期用於確定用戶是否登錄。所以你可以在代表這個狀態的會話中設置一個標誌。如果缺失(用戶還沒有登錄或會話超時),他被認爲沒有登錄。

1

這取決於他們是如何「登錄」在首位。服務器上的會話過期不會爲您做到這一點嗎?如果你真的想這樣做手工,那麼你可以使用一個setTimeout的一些JavaScript,但通過把這樣的事情在你的HTML HEAD部分多數民衆贊成這樣做是醜陋

4

一個非常簡單有效的方法:

<META HTTP-EQUIV="refresh" CONTENT="1800;URL=logout.php?timeout"> 

用適當的腳本替換logout.php?timeout。在上面的例子中,如果?timeout在查詢字符串中,我向他們顯示一個登錄頁面,其中包含表明他們已經由於不活動而被註銷的信息。

用您希望允許它們在自動註銷之前保持非活動狀態的時間以秒爲單位替換1800。將此設置爲您將會話過期設置爲的時間。

編輯 - 另一個簡單的機制是實現一個名爲last_time或last_activity的會話變量或沿着這些行的東西,並在每次有活動時將其設置爲時間戳。在我的大部分內容中,我都有一個通用包含文件,可以在同一個文件中進行檢查,以確保它處於您爲活動會話設定的約束範圍內。如果時間太長 - 只需做一次300重定向到註銷頁面並在那裏顯示適當的不活動消息。

祝你好運!

伊恩

0

你可以有一些JavaScript,檢查服務器每隔x分鐘時看到該用戶的最後一項活動了。不應該超過幾行代碼。如果用戶禁用JavaScript,我還會添加元刷新。

13

嗨,夥計,這是我使用的代碼。這不是我的,但我確實修改它以達到「完美」。

// Add the following into your HEAD section 
var timer = 0; 
function set_interval() { 
    // the interval 'timer' is set as soon as the page loads 
    timer = setInterval("auto_logout()", 10000); 
    // the figure '10000' above indicates how many milliseconds the timer be set to. 
    // Eg: to set it to 5 mins, calculate 5min = 5x60 = 300 sec = 300,000 millisec. 
    // So set it to 300000 
} 

function reset_interval() { 
    //resets the timer. The timer is reset on each of the below events: 
    // 1. mousemove 2. mouseclick 3. key press 4. scroliing 
    //first step: clear the existing timer 

    if (timer != 0) { 
    clearInterval(timer); 
    timer = 0; 
    // second step: implement the timer again 
    timer = setInterval("auto_logout()", 10000); 
    // completed the reset of the timer 
    } 
} 

function auto_logout() { 
    // this function will redirect the user to the logout script 
    window.location = "your_logout_script.php"; 
} 

// Add the following attributes into your BODY tag 
onload="set_interval()" 
onmousemove="reset_interval()" 
onclick="reset_interval()" 
onkeypress="reset_interval()" 
onscroll="reset_interval()" 

咕運氣

理查德

3

我們可以改善我們的代碼的jQuery現在


idleTime = 0; 
$(document).ready(function() { 

    var idleInterval = setInterval("timerIncrement()", 60000); // 1 minute //60000 
    $(this).mousemove(function(e) { 
     idleTime = 0; 
    }); 
    $(this).keypress(function(e) { 
     idleTime = 0; 
    }); 

}); 

function timerIncrement() { 

    idleTime = idleTime + 1; 

    if (idleTime >= 5) { 
     window.location = $('#base_url').val() + 'home/logout_user'; 
    } 
} 

2

你可以更優雅與underscore和做JavaScript的libraries-

$('body').on("click mousemove keyup", _.debounce(function(){ 
    // logout user here 
}, 1800000)) // 30 minutes inactivity 
0

我把時間戳「現在」,並檢查每個點擊如果延遲超過百億秒低於3000秒以上,這意味着用戶只需登錄,如果不是它會重定向到註銷

var time = 0; 

$(document).on('click', function() { 

    var now = Date.now()/1000 | 0; 

    if (now - time < 3000 || now - time > 1480000000) { 
     time = now; 
    } else { 
     window.location.replace("http://url"); 
    } 
}) 
相關問題