Executing code when the user hits the X button on the browse

I am writting an app that is going to record login times, logout times
and the session data of a user of the system. This will work if the
user manually logs out, but I would like it to record when the user
quits if the users exits the browser (or goes to a page not in our
application). Any ideas on how to accomplish this task?

Any help is appreciated.

Well, there’s no good way to do that easily or perfectly. But here are
some ideas.

You could try to do fancy javascript to ‘catch’ the closing of the
window. In my experience, that’s not really that reliable though, so I
forget how you do that, because I generally don’t try.

If you store your sessions in the database and make sure there’s an
updated_at column, then the updated_at column will the last time the
user browsers sent a request to your app. So if they didn’t manually log
out, you could look at that updated_at field. You could have a cron-job
which periodically went through and says "if the user didn’t manually
log out, but their session hasn’t been updated in an hour (or whatever),
we’ll set their log out time to equal the session updated_at.

But let’s say a user is staring at the same screen for an hour—the
session updated_at might be an hour old, but they’re still staring at
the screen. Well, you could use periodically_call_remote to have an AJAX
function called every five seconds or whatever. As long as they are
looking at that page, the AJAX function will be called every five
seconds. If they stop, it will stop being called. So what does the
function do? It doens’t have to do anything, as long as it contacts
the server, session.updated_at will be updated, and then see the above
paragraph again.

Hope this gives you some ideas.

Jonathan

White W. wrote:

I am writting an app that is going to record login times, logout times
and the session data of a user of the system. This will work if the
user manually logs out, but I would like it to record when the user
quits if the users exits the browser (or goes to a page not in our
application). Any ideas on how to accomplish this task?

Any help is appreciated.

On 20 Sep 2007, at 16:58, White W. wrote:

I am writting an app that is going to record login times, logout times
and the session data of a user of the system. This will work if the
user manually logs out, but I would like it to record when the user
quits if the users exits the browser (or goes to a page not in our
application). Any ideas on how to accomplish this task?

There’s not out-of-the-box way (javascript doesn’t work, at least not
for now), but you could do it with Juggernaut if it’s really
important to you: http://ncavig.com/blog/?p=30#more-30
Haven’t tried it out, but it seems very nice.

Best regards

Peter De Berdt