Given a certain amount of time before the session expires, a dialog is shown to the user with the option to either log out now, or stay connected.
If log out now is selected, the page is redirected to a logout URL. If stay connected is selected, a keep-alive URL is requested through AJAX. If no option is selected before the session expires, the page is automatically redirected to a timeout URL.
The time left of the session is retrieved from the server through ajax in json format or through configuration
This implementation with its default values works nicely with multiple tabs (as is the server who controls the time left the session has)
The example uses PHP. Open default.php to view.
- Include jQuery
- Include jQuery UI (for dialog)
- Include jquery.sessionTimeoutHandler.js
- Call
$.sessionTimeoutHandler();
after document ready
- title
Title of the dialog window shown to the user. Default: 'Your session is about to expire.'
- message
Text of the dialog window shown to the user. Default: 'Your session is about to expire.'
- keepAliveUrl
URL to call through AJAX to keep session alive. This request resets the session timeout. Default: 'keepAlive.php'
- retrieveTimeLeftUrl
URL to call through AJAX to get how much time left this session has. This request doesn´t update the session timeout Default: 'keepAlive.php'
- redirUrl
URL to take browser to if no action is take before session times out Default: 'timedOut.php'
- logoutUrl
URL to take browser to if user clicks "Log Out Now" Default: 'logout.php'
- defaultSessionTime
Default session timeTime in milliseconds.
If set to false, the time will be retrieved from the server via retrieveTimeLeftUrl.
Default: false
- warnWhenLeft
Time in milliseconds before the session expires to open the dialog Default: 300000 (5 minutes)
- checkTimeBeforeRedirect
If true, before issuing a redirect to redirUrl, will ask the server if the session has time left. If it has, will reset the timers accordingly. This way different tabs will be synchronized
Default: true
- errorGettingSessionTimeLeft
Message to alert the user if there was an error retrieving the time left from the server
Default: There was an error retrieving session information from the server. Session could be timed out
- modalId
Id of the modal window generated
Default: sessionTimeout-dialog
Default - Get Time left from server. Send Keep Alive. Check if session still active before redirect
$('body').sessionTimeoutHandler();
Just timer - Don't send keepAlive to server - Don't bring time left from server
$('body').sessionTimeoutHandler({
redirUrl : 'timedOut.php',
logoutUrl : 'logout.php',
warnWhenLeft : 10000,
defaultSessionTime: 20000,
checkTimeBeforeRedirect: false,
getSessionTimeLeft: function ( event, data ){
data.callback.call(data.plugin, data.plugin.options.defaultSessionTime);
}
});
Keep Alive - But don't bring time left from server
$('body').sessionTimeoutHandler({
keepAliveUrl : 'keepAlive.php',
redirUrl : 'timedOut.php',
logoutUrl : 'logout.php',
defaultSessionTime: 20000,
warnWhenLeft : 10000,
checkTimeBeforeRedirect: false,
getSessionTimeLeft: function ( event, data ){
$.ajax({
context: data.plugin,
dataType: "json",
url: data.plugin.options.keepAliveUrl,
success: function(){
data.callback.call(data.plugin,
data.plugin.options.defaultSessionTime);
},
error: function(){
alert(data.plugin.options.errorGettingSessionTimeLeft);
}
});
}
});