1

If user is inactive for some specific duration, then it should autometically log out. So how I can do this using codeigniter? OR how to check whether user is active or not after login on that site?

nilesh
  • 511
  • 2
  • 5
  • 10
  • Question shows no research effort. Please show your work. – Dogoferis Apr 22 '13 at 14:22
  • Read CI documentation its for reading.. :) http://ellislab.com/codeigniter/user-guide/libraries/sessions.html $this->session->sess_destroy(); – Svetoslav Apr 22 '13 at 14:26
  • be careful not to implement in the data entry page where the user might need some time to input the data over some time e.g. 30 minutes (in my case) – 4 Leave Cover Dec 21 '16 at 06:31

3 Answers3

9
// 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()"
nilesh
  • 511
  • 2
  • 5
  • 10
  • 6
    It has been copied from: http://stackoverflow.com/questions/572938/force-logout-users-if-users-are-inactive-for-a-certain-period-of-time That is not the we to post answers! – Lior Elrom Apr 24 '13 at 18:04
1

You can save the time that your user logged-in in a session or a cookie

Example: $this->session->set_userdata('time', time()); and use a javascriptjQuery function (Exp. $.getJSON('time.php', function (data) {alert(data.serverTime);});) or anything else to check the current time. Then, log your user out when needed.

However, next time, please place code or something else that shows your efforts.

Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
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.
    }
?>

... which was originally taken from skillrow.com/log-out-user-if-user-is-inactive-for-certain-time-php/ (now 404).

Matt
  • 74,352
  • 26
  • 153
  • 180
kamal
  • 5
  • 1
  • @Matt: as per your code user will be logged out after x minutes, 3 minutes in this case, even if user is active. Here is my modified solution: $minutes = 3; if(isset($_SESSION['time']) && (time()-$_SESSION['time'] > $minutes*60)){ header('location:'. 'logout_url.php'); die(); } // set time if empty and user is logged in if($userLoggedIn){ $_SESSION['time'] = time(); } – shairya Aug 10 '17 at 08:22
  • @shairya: it is not my code, it's kamal's. I only [edited his answer](https://stackoverflow.com/posts/23903794/revisions) – Matt Aug 10 '17 at 08:25