Jump to:

23001 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Automatically logout an inactive user

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

Page: 1
Go to End
Author Topic: 857 Views
  • novaweb
    Avatar
    Community Member
    109 Posts

    [SOLVED] Automatically logout an inactive user Link to this post

    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

  • novaweb
    Avatar
    Community Member
    109 Posts

    Re: [SOLVED] Automatically logout an inactive user Link to this post

    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());
       }

    857 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.