admin管理员组

文章数量:1292316

As some of you probably know, Facebook is using this kind of "system" where a popup is displayed when a user session is lost due to inactivity or distant session close. I already saw and read this Node.js question but didn't find anything.

I am working for a Canadian puter business our main product is a CRM and everything is coded using Classic ASP.

I know.

The whole web-based application is working great and since we host the site on our servers, it is possible if necessary to open ports and use sockets.

Here goes the main question: is there a way (using a javascript library or a jQuery plug-in maybe?) to trigger a client-side event when the session expires or is simply lost due to a server reset for example?

Of course, the best would be to use another solution than sending an AJAX request every second to validate if the user session still exists. If it can help, there is a maximum of about 3'500 users connected at the same time and our servers can easily handle more traffic. The servers are working on Windows Server 2008 along with IIS 7.

Unfortunately, I cannot provide any code blocks or screenshots for this question since there is nothing to debug.

One idea would be to use an AJAX request to a file that does not return anything and hangs there. If session is lost (inactivity or server reset), the AJAX request will trigger an error and the "error" function will be triggered. Would that be something to consider?

Or else, any other suggestions?

As some of you probably know, Facebook is using this kind of "system" where a popup is displayed when a user session is lost due to inactivity or distant session close. I already saw and read this Node.js question but didn't find anything.

I am working for a Canadian puter business our main product is a CRM and everything is coded using Classic ASP.

I know.

The whole web-based application is working great and since we host the site on our servers, it is possible if necessary to open ports and use sockets.

Here goes the main question: is there a way (using a javascript library or a jQuery plug-in maybe?) to trigger a client-side event when the session expires or is simply lost due to a server reset for example?

Of course, the best would be to use another solution than sending an AJAX request every second to validate if the user session still exists. If it can help, there is a maximum of about 3'500 users connected at the same time and our servers can easily handle more traffic. The servers are working on Windows Server 2008 along with IIS 7.

Unfortunately, I cannot provide any code blocks or screenshots for this question since there is nothing to debug.

One idea would be to use an AJAX request to a file that does not return anything and hangs there. If session is lost (inactivity or server reset), the AJAX request will trigger an error and the "error" function will be triggered. Would that be something to consider?

Or else, any other suggestions?

Share edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Aug 8, 2013 at 14:43 Érik DesjardinsÉrik Desjardins 9931 gold badge13 silver badges25 bronze badges 4
  • Does it have to be when page is inactive and nothing is going on? You can check for session timeout when user attempts do do something (click a button, navigate somewhere etc.) – Yuriy Galanter Commented Aug 8, 2013 at 17:41
  • 3 Also keep in mind, that checking via AJAX every second would refresh the session and effectively PREVENT the session from timeout-ing. – gpinkas Commented Aug 9, 2013 at 8:19
  • @gpinkas Indeed, I need something like an AJAX request done on page load that never returns anything and on error (due to server reset or session timeout) prompts user. Would that be a solution? – Érik Desjardins Commented Aug 12, 2013 at 14:58
  • @YuriyGalanter It has to be "real-time", the user has to be notified the moment the session is lost. – Érik Desjardins Commented Aug 12, 2013 at 15:02
Add a ment  | 

4 Answers 4

Reset to default 3

One way to do it is to set client-side timer set to the same time as session expiration time.

Let's say your session is set to expire after 20 minutes. When page loads - client-side timer set to 20 minutes kicks in. If user does any server interaction (submits form etc.) - timer is reset. But if nothing happens during this 20 minutes - timer counts down and you get your event.

You could do the following to achieve this, assuming you have a default session timeout of 20:00 minutes:

  1. Ensure, that each user has a "session cookie" issued by you, NOT the default ASP Session Cookie.

    dim live_session_id
    live_session_id = Request.Cookies("livesession")
    if live_session_id = "" then
        live_session_id = create_unique_session_id()
        Response.Cookies("livesession") = live_session_id
    end if
    
  2. Save this live_session_id in a database along with the expected session expire date

    call updateSession(live_session_id, dateadd("n", 20, now())) ' = now()+20min
    
  3. Output the live_session_id somewhere on your page, so you can access it via JS.

  4. Implement a serverside ASP script that checks the current session state for a given live_session_id and make it accessible in IIS in a DIFFERENT subdomain, so calls to this check will NOT refresh the ASP session. The script could return the time difference between now and session end, so you could output the duration the session will remain valid.

  5. Add some AJAX code to call the check script every other second, so you could issue a warning if the session time draws to an end.

  6. To make it detect IIS reset, you could clear the saved sessions in the database by implementing Application_OnStart in global.asa. This way, your clients would detect a session loss by IIS reset.

another quick and dirty method

On every page load, you could let a javascript count down from 20:00 minutes and display a session lost warning after this time. This is what my online banking system uses... :)

as far as i understand you the main Problem is that the user has to fill out enormous forms. that could take some time and during that time the session could expire.

furthermore the session could be ended by anything else (iisreset or so) during the time the user fills out the form.

in my understanding you do not have to notify the Client that the session is lost/expired/ended/abandoned. it would be enough to just show a Login form (Ajax or something) when the user submits the form OR the next request (by Ajax as you mentioned) is made by the Client.

the called asp script checks if the session is valid and if not a popup or overlay is shown to Login the user by Ajax first and the form is submitted afterwards.

you could think of a http Status code 401 or something to send back to the Client and the Client then Shows the mentioned Ajax Login form...

What makes a session expire in your CRM? Expiring after X time passes since last [user] action is pretty conventional and will allow you to use ajax to keep the session alive. Let's say a session is good for 5 minutes as part of security requirements for super-secret NSA banking CRM with kittens and youtube videos. A good scenario of how a session can be extended would be this:

  1. a page is opened, validating the session for another 5 minutes
  2. a timeout is set with JS to make an ajax request every 4 minutes
  3. [4 minutes later] the request is made, returning a very light-weight response.
  4. if the response says everything is ok and the session is still valid, schedule another "ping" and carry on as usual. If the response es back with an error (session invalidated on the server because of logging in from a different PC etc.), gracefully handle it. Allow the users to retain what they were working, don't just kick them out to a log in screen with an error
  5. User navigates away from the page (clicks a link, submits a form), repeat from the beginning. If the user navigates to an external site or closes the browser, his session will self-destruct in no more than 5 minutes :)

Obviously you can piggy-back any additional information onto the ajax call in step 3 - e.g. notifying the user of new items assigned to them in CRM?

Google is your friend, one of the first results gives a not bad overview of the approach basics.

本文标签: javascriptTrigger client side event when session in lostStack Overflow