Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » How to detect how long the user is logged in

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: 303 Views
  • BenWu
    Avatar
    Community Member
    87 Posts

    How to detect how long the user is logged in Link to this post

    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

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: How to detect how long the user is logged in Link to this post

    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

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: How to detect how long the user is logged in Link to this post

    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.

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: How to detect how long the user is logged in Link to this post

    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.

    303 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.