|
Want to help users be mindful of time
left in session after logged in?
One "problem" with a straight javascript
timer is that each time the page on which the timer is displayed is
reloaded, the timer resets. My little ASP scripting here is a way to carry
the current amount of time left in a session over to a timer reload.
On your login processing form, add something
like this above the existing session timeout, if any:
Session("STRLOGINTIME")=now()
'this puts the date-time at login into a session variable
Session.Timeout=90 ' if you don't
already have a session timeout set...this one would be for 90 minutes. The
demo at the top of this page has a 6 min session.
Then, follow the instructions for placement of the following javascript, as found at
http://javascript.internet.com/page-details/session-time-out.html with the small
addition of ASP made near the bottom:
min = <%=TimeOnClock%>
* Minutes(document.timerform.clock.value);
<html>
<% ' place this ASP code shown here in red, at
the top of your header file above the javascript
Session("TIMEexpiredFromSession") = (DateDiff("n",(Session("STRLOGINTIME")),now()))
'this gets how many minutes have passed since login
TimeLeftinSession = (Session.Timeout)-(Session("TIMEexpiredFromSession"))
'this gets how many minutes are remaining in the session
TimeOnClock = TimeLeftinSession/2 'Get the correct value
for the javascript timer
TimeOnClock = FormatNumber(TimeOnClock,0) 'Convert it to a whole
number
%>
<head>
<title>Your Page Title</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore, Editor -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
// Take user here after session timed out
timedouturl = "logout.asp";
function Minutes(data) {
for (var i = 0; i < data.length; i++)
if (data.substring(i, i + 1) == ":")
break;
return (data.substring(0, i));
}
function Seconds(data) {
for (var i = 0; i < data.length; i++)
if (data.substring(i, i + 1) == ":")
break;
return (data.substring(i + 1, data.length));
}
function Display(min, sec) {
var disp;
if (min <= 9) disp = " 0";
else disp = " ";
disp += min + ":";
if (sec <= 9) disp += "0" + sec;
else disp += sec;
return (disp);
}
function Down() {
sec--;
if (sec == -1) { sec = 59; min--; }
document.timerform.clock.value = Display(min, sec);
window.status = "Session will time out in: " + Display(min, sec);
if (min == 0 && sec == 0) {
alert("Your session has timed out.");
//window.location.href = timedouturl;
window.top.location.href =timedouturl;
}
else down = setTimeout("Down()", 1000);
}
function timeIt() {
min = <%=TimeOnClock%> * Minutes(document.timerform.clock.value);
sec = 0+ Seconds(document.timerform.clock.value);
Down();
}
// End -->
</script>
</head>
<body OnLoad="timeIt()">
<center>
<form name="timerform">
<input type="text" name="clock" size="7" value="2:00"> minutes
remaining before login expires
</form>
<font face="arial, helvetica"
size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center>
|