Tuesday, March 17, 2009

Drupal Auto logout using custom code

Drupal Auto logout using custom code without autologout module
Add this line in the nodeapi to get last access time

switch ($op){
case "insert":
case "update":
case "execute":
case "validate":
case "prepare":
case "delete":
$_SESSION['lastaccess'] = time();
break;
}

Set $auto_timeout in settings.php

global $auto_timeout;
$auto_timeout=900;

Add the following code to check the time in hook_footer
function <modulename>_footer(){
global $user;
global $auto_timeout;
if($user->uid>0){
if (!isset($_SESSION['lastaccess']) || ((int)$_SESSION['lastaccess'] + $auto_timeout) > time()) {
$_SESSION['lastaccess'] = time();
return;
}else {
unset($_SESSION['lastaccess']);
drupal_set_message("Session timed out");
user_logout();
exit;
return;
}
}
return;
}

1 comment: