Albert
Albert

Reputation: 1526

CakePHP (or just PHP) get session duration

Is there any way to get the duration of the user's session?

If the user logs in I can save the initial time. If he logs out, I can get the final time and calculate the duration of the session.

What if the user doesn't log out and the session just expires? Is there any way to know if/when that happens?

Upvotes: 0

Views: 270

Answers (2)

gview
gview

Reputation: 15391

No, not really. HTTP is session less. People who really want a more granular idea of active users will frequently use some sort of heartbeat technology, that creates a persistent connection or simulates it with polling, using ajax, or a flash movie, or websockets.

PHP sessions are really more of a mechanic for linking some temporary serverside storage with a specific client/browser instance, despite the implication some might read into the name.

Upvotes: 0

jtavares
jtavares

Reputation: 449

There is no way to know if the session is still active, so the best thing you can do is save the SESSION_START_TIME on you Database and on every request log a LAST_REQUEST_TIME.

that way you know when was the start and the "possible" end of the session.

you can also know how many active sessions there are right now, by checking all sessions with LAST_REQUEST_TIME<5minutes... so its not a bad solution.

Upvotes: 2

Related Questions