2013-04-22 27 views
0

如果用戶在某個特定的持續時間內處於非活動狀態,則應該自動註銷。那麼我怎樣才能使用codeigniter來做到這一點? 或 如何在用戶登錄後檢查用戶是否活動?如果用戶在某個特定的持續時間內處於非活動狀態,使用codeigniter如何自動註銷

+0

問題顯示沒有研究工作。請展示你的工作。 – Dogoferis 2013-04-22 14:22:59

+0

閱讀CI文檔其閱讀.. :) http://ellislab.com/codeigniter/user-guide/libraries/sessions.html $ this-> session-> sess_destroy(); – Svetoslav 2013-04-22 14:26:38

+0

小心不要在數據輸入頁面中實現,用戶可能需要一些時間在一段時間內輸入數據,例如, 30分鐘(在我的情況下) – 2016-12-21 06:31:11

回答

4
// 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()" 
+5

它已被複制:http://stackoverflow.com/questions/572938/force-logout-users-if-users-are-inactive-for-a-certain-period-of-時間那不是我們要發佈的答案! – 2013-04-24 18:04:00

1

您可以保存您的用戶登錄的sessioncookie

實例時間:$this->session->set_userdata('time', time()); 並使用javascriptjQuery函數(試驗$.getJSON('time.php', function (data) {alert(data.serverTime);});)或其他任何檢查當前時間。然後,在需要時將用戶註銷。

但是,下次請放置代碼或其他可顯示您的工作的其他內容。

0
<?php 
    $minutes=3;//Set logout time in minutes  
    if (!isset($_SESSION['time'])) { 
     $_SESSION['time'] = time(); 
    } else if (time() – $_SESSION['time'] > $minutes*60) { 
     session_destroy(); 
     header(‘location:login.php’);//redirect user to a login page or any page to which we want to redirect. 
    } 
?> 

...它最初是從 skillrow.com/log-out-user-if-user-is-inactive-for-certain-time-php/(現爲404)拍攝。

+0

@Matt:根據您的代碼,用戶將在x分鐘後註銷,在這種情況下3分鐘,即使用戶處於活動狀態。這是我修改後的解決方案:$ minutes = 3; ('location:'。''logout_url.php'');(如果(isset($ _ SESSION ['time'])&&(time() - $ _ SESSION ['time']> $ minutes * 60)){ die(); } //設置時間如果爲空並且用戶已登錄 if($ userLoggedIn){ $ _SESSION ['time'] = time(); } – shairya 2017-08-10 08:22:59

+0

@shairya:這不是我的代碼,它是kamal的。我只[編輯他的回答](https://stackoverflow.com/posts/23903794/revisions) – Matt 2017-08-10 08:25:15

相關問題