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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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