Many people want to use response.redirect in Session_OnEnd() to trap the session event (whether to give the user a prettier message than telling them they're not logged in, or to clean up database activity, etc). As explained in
Article #2078, Session_OnEnd() is unreliable at best.
Here's one idea. Use client-side script to send the user a warning just before the timeout. The following example, used on each page, demonstrates how to warn the user two minutes before their session will expire. You are free, obviously, to change the code that happens within the JavaScript (e.g. call a function), and to change the advanceWarning value.
<% advanceWarning = 2 jsTimeout = (session.timeout - advanceWarning) * 60000 %> <script> window.setTimeout("alert('Session is about to expire');",<%=jsTimeout%>); </script> |
This example assumes the page is sitting idle, and doesn't have any components (FRAMEs, parent documents, IFRAMEs, IMGs) that are interacting with the server. In those cases, users might be warned far earlier than their session will actually time out.
You might be careful if you use this kind of code (say at exactly the session timeout, rather than two minutes before) to clean up database or other session data by calling a separate ASP file from JavaScript (e.g. in a hidden IFRAME, new window, or even by changing the SRC attribute of an IMG). The JavaScript code will only fire if the browser window is still open (also a limitation of Session_OnEnd()).