nilesh
nilesh

Reputation: 531

how to auto log out if user is inactive for some specific duration using codeigniter

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?

Upvotes: 1

Views: 19286

Answers (3)

kamal
kamal

Reputation: 5

<?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).

Upvotes: 0

nilesh
nilesh

Reputation: 531

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

Upvotes: 9

Lior Elrom
Lior Elrom

Reputation: 20852

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.

Upvotes: 1

Related Questions