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

How to detect how long the user is logged in


Go to End


4 Posts   709 Views

Avatar
BenWu

Community Member, 97 Posts

21 May 2013 at 10:11am

Hello

I wonder if there is a way in SS to tell how long the user logged in from the front end? I will then record this and show it in the CMS backend.

thanks

Ben

Avatar
kinglozzer

Community Member, 187 Posts

22 May 2013 at 8:25pm

Hi Ben,

Not something I've attempted before, but I believe you can store login attempts by using:

Security::set_login_recording(true);

Perhaps you could use this data? Something along the lines of:

$lastLoggedIn = LoginAttempt::get()->addFilter('MemberID', Member::currentUserID())->addFilter('Status', 'Success')->sort('LastUpdated DESC')->first()->LastUpdated;

Then work out the difference between that timestamp and the current time. Hope this helps

Avatar
BenWu

Community Member, 97 Posts

22 May 2013 at 9:48pm

Security::set_login_recording(true);

will only make sure SS to record every login attempt. However, it will not record how long a user login for one session.

I think we need some Ajax script to 'ping' the server and tell the session is still lived and record the time in database. Then the backend will work out this recorded time and the last login attempt to tell how long the user is logged in.

but i can see there is a problem when a user logins from multiple browsers.

Avatar
kinglozzer

Community Member, 187 Posts

22 May 2013 at 10:27pm

Hi Ben,

Yes I know that it will only record the login, not how long they're logged in for, but you can use that time as the 'start' point - i.e. when they first logged in.

If you need to record how long they're logged in for every single time they log in, that's a lot more complicated.