/    Sign up×
Community /Pin to ProfileBookmark

Inactivity check, with confirm box. Help! =)

Hey everyone,

What I’m trying to achieve is a script that will redirect the user to the login page after 5 minutes of inactivity. I can achieve this using an alert box, but since the content on the page that they are viewing contains video (all under 3 minutes) I wanted to give them the option of staying logged in.

Here is the current code that I’m using

[CODE]<!– Begin
// Take user here after session timed out
timedouturl = “/nfinanse/index.php”;

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) {
var r=confirm(“You have been inactive 5 min. Stay online?”)
if (r==true)
{
// I was using a reload command, but I noticed it’s not resetting the value in the form (which is how the entire thing is monitored)
// document.location.reload();
}
else
{
window.location.href = timedouturl;
}
}

else down = setTimeout(“Down()”, 1000);
}
function timeIt() {
min = 1 * 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=”hidden” name=”clock” size=”7″ value=”0:15″><p>
</form>
</center>[/CODE]

It’s achieving everything that I want, but now the only thing I’m stuck on is how do I reset the timer correctly if they hit ok? A refresh right now will return some really strange values if you make the hidden field a text field. I could just leave it blank but that lets them stay logged on as long as they want without being redirected.

Hopefully I was clear in explaining my problem, any help would be greatly appreciated!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@nafetskiauthorOct 22.2007 — Wooohoo!

I got it working, figured I'd post it here in case anybody else has had questions about it.

It was treating a page refresh differently than if you were to resubmit the URL back to the browser. (not sure why it was doing that, I figured the two were one in the same...but if I clicked on my bookmark with the page I was trying to load it would reload properly)

So, to get around that.

[CODE]
<?php
// Retrieves the current page and saves it to a variable
$currentpage = $_SERVER['PHP_SELF'];
?>
[/CODE]


Then for the confirm box, I just changed it to this...

[CODE]
var r=confirm("You have been inactive 5 min. Stay online?")
if (r==true)
{
var currentpage="<?php $currentpage ?>";
window.location.href = currentpage;
}
else
{
window.location.href = timedouturl;
}
}
[/CODE]


Worked like a charm! Anyway, just posting this since I saw a lot of people looking for the same solution...that didn't require any AJAX or ASP. It's sort of ghetto, but making the form field hidden it works just fine =D
×

Success!

Help @nafetski spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...