Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

[SOLVED] Automatically logout an inactive user


Reply


2 Posts   1086 Views

Avatar
novaweb

Community Member, 115 Posts

5 March 2012 at 11:26am

Hi,

Does anyone know how to log out a user who is inactive, for say 1 hour? This is not just for the CMS, but the frontend too.

Cheers,
Josh

Avatar
novaweb

Community Member, 115 Posts

5 March 2012 at 12:08pm

Edited: 05/03/2012 1:32pm

Have come up with this solution, seems to work well.

In your Page.php Page_Controller class, set up an init function:

function init() {
parent:: init();
self::logoutInactiveUser();
}

then put this below the init function:

   function logoutInactiveUser() {
      $inactivityLimit = 30; // in Minutes
      $inactivityLimit = $inactivityLimit * 60; // Converted to seconds
      $sessionStart = Session::get('session_start_time');
      if (isset($sessionStart)){
       $elapsed_time = time() - Session::get('session_start_time');
       if ($elapsed_time >= $inactivityLimit) {
       $member = Member::currentUser();
            if($member) $member->logOut();
            Session::clear_all();
       Director::redirect(Director::baseURL() . 'Security/login');
       }
      }
      Session::set('session_start_time', time());
   }