Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Session trap to log user offline
  • hello,

    Me again :)
    I have an application where we consider that the user is online while the session is live.
    I want to log in a database the timestamp when the user is not online anymore.
    I thought a bout using events like session_close or similar events, but don't know how to trap/use this.
    Please note that my configuration is set to close the session even on browser close and I want that this is also logged the same way in my database.

    Any idea/advice ? Best practice ?

    Thanks for your help,
    Herve.
  • HarroHarro
    Accepted Answer
    Very difficult to do, because the session is live until it is either destroyed or expired.

    A destroy you should be able to capture, but an expiration is impossible to capture with certain backends. Some, like Memcached or Redis just auto expire the data, without the application being aware.

    In your case, the session not destroyed but dereferenced because you delete the session cookie client-side. There is also no way for the application to know this has happened.

    We do it the other way around. Our user records have two additional fields (in EAV since we use Ormauth).

    One to log the "last-seen" timestamp, which is updated on every request whenever Auth::check() happens to log the user back in from the existing session, and one to log the "last-login" timestamp, updated after a valid login, and displayed in the user profile (so the user can check if the account has accessed by someone else).

    Based on last-seen, you could easily calculate users-online, which is last-seen + 15 min > time().
  • Thank you Harro, that helps us to define the way we are going to manage users.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion