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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

[SOLVED] Automatically logout an inactive user


Go to End


2 Posts   3355 Views

Avatar
novaweb

Community Member, 116 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, 116 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());
	}