Page 1 of 1
Static 20 countdown timer...
Posted: Wed Mar 28, 2012 12:27 pm
by orbdrums
I have some code that logs a user out after 20 minutes of inactivity but I can't seem to get the countdown display to update on the page. It will display the time left but only when the page is refreshed. Here is the code that logs the user out and will echo the "session_life_diff".
Code: Select all
<?php
session_cache_expire( 20 );
session_start();
$inactive = 1200;
if(isset($_SESSION['start']) )
{
$session_life = time() - $_SESSION['start'];
$session_life_diff = $inactive - $session_life;
echo $session_life_diff / 60;
if($session_life > $inactive)
{
header("Location: ../logout.php");
}
}
$_SESSION['start'] = time();
if($_SESSION['valid_user'] != true)
{
header('Location: ../start.php');
}
?>
Any suggestions? Thanks.
Re: Static 20 countdown timer...
Posted: Wed Mar 28, 2012 2:14 pm
by requinix
You have to use JavaScript for it.
Do you really need a running countdown on the page? It's kinda scary. What if you, say, displayed a message after ~18 minutes saying their session is about to expire?
Re: Static 20 countdown timer...
Posted: Wed Mar 28, 2012 2:33 pm
by orbdrums
That would be fine. I just can't use this type of refresh <meta http-equiv="refresh" content="1205"> because I'm using it after the logout.php. Any suggestions would be greatly appreciated.
Re: Static 20 countdown timer...
Posted: Wed Mar 28, 2012 2:47 pm
by requinix
orbdrums wrote:I just can't use this type of refresh <meta http-equiv="refresh" content="1205"> because I'm using it after the logout.php.
I don't understand that, but okay.
For an actual countdown you have code that looks like
Code: Select all
var counter = 60*20;
var interval = window.setInterval(function() {
counter--;
if (counter <= 0) {
window.clearInterval(interval);
// redirect to logout page
} else {
var timeleft = /* reformat the number into something decent, like "19 minutes" */
document.getElementById("timer").innerHTML = timeleft;
}
}, 1000);
For the thing I mentioned it'd look like
Code: Select all
window.setTimeout(function() {
alert(/* some message about logging out in two minutes */);
window.setTimeout(function() {
// redirect
}, 2*60 * 1000);
}, 18*60 * 1000);
Re: Static 20 countdown timer...
Posted: Wed Mar 28, 2012 2:56 pm
by orbdrums
Thanks a lot for the help. I know next to nothing about JavaScript so this will be quite the learning experience. Can I create a separate .js file that contains your code and then call from my .php protected files? And, if so, what would be the proper syntax for a JavaScript file call? Thanks again.
Re: Static 20 countdown timer...
Posted: Wed Mar 28, 2012 3:28 pm
by requinix
You can stick the code in a file but only the second bit of code can be copy/pasted. The first one needs a little bit of work before it can be used.
Create a .js file somewhere and include it in the HTML output with a
Code: Select all
<script type="text/javascript" src="/path/to/file.js"></script>
at the bottom of the <head> section.
If you want the first one, question: are you using any JavaScript framework like jQuery or YUI or one of the million others?
Re: Static 20 countdown timer...
Posted: Wed Mar 28, 2012 9:58 pm
by orbdrums
The second one you suggested will be just fine. I don't even know what a JavaScript framework is so I'm going to say no. Just so I'm clear, I can create the .js file and then add the <script> tag in each file that I'm calling the .js file? And the redirect that is commented out in your second bit of code should redirect where? Thanks again for your help.
Re: Static 20 countdown timer...
Posted: Thu Mar 29, 2012 10:32 am
by requinix
Yes, and wherever you want to send the user when they're supposed to be logged out (logout.php I think you said).
Re: Static 20 countdown timer...
Posted: Thu Mar 29, 2012 12:01 pm
by orbdrums
Cool. It works and thanks for your help!