views:

58

answers:

2

Hello Everyone,

Is there any way to detect session timeout without (user interaction)*, and redirect it to some page; i.e. if there is no activity on page @ specific duration; server detects it and redirect it automatically on some other.

By user user interaction I mean; there is a way to detect session timeout when user clicks on something, then some request goes to server and then server checks if current user session is expired or not.

What I need here is that we don't inform server anything (or we don't perform any action), but when session expires server detects it automatically and perform required action.

Thanks, Raza

+1  A: 

You can of course do such a thing in JavaScript by implementing a document-wide keyboard and / or mouse listener and a periodical method with a timeout.

var timeOut = 1000 * 60 * 30; // 30 minutes
var lastActivity = new Date().getTime();
var checkTimeout;
checkTimeOut = function(){
    if(new Date().getTime() > lastActivity + timeOut){
        // redirect to timeout page
    }else{
        window.setTimeout(checkTimeOut, 1000); // check once per second
    }
}

now your global listeners just have to set lastActivity to the current time on every action.

On re-reading the question, you want to use the actual session timeout from the application server. That's a tough one, because your when you send ajax requests to the server you will actually keep the session from expiring (unless there is a hard limit), so my answer might still be the best way to do it.

seanizer
I have used a method like this in the past and it works well. +1
cjstehno
thanks, I really needed this thing, btw I have also found related link on the same site http://stackoverflow.com/questions/1985250/notify-user-about-session-timeout-in-j2ee
A: 

Your question need editing to be human-readable.

  1. detect session timeout without (user interaction) --> lol!

You could set up a thread to list all active sessions, but seems to me you are trying something that could be done a much more intelligent way.

Raul Lapeira Herrero
@Raul, your answer is not *that* readable and understandable, either, and 'lol' is definitely not appropriate here (it's both slang and rude).
seanizer
lol about that comment, come on, I code for fun, leave politics for politicians :)
Raul Lapeira Herrero