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

How to detect how long the user is logged in


Reply


4 Posts   363 Views

Avatar
BenWu

Community Member, 91 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, 171 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, 91 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, 171 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.