Static 20 countdown timer...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Static 20 countdown timer...

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Static 20 countdown timer...

Post 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?
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Static 20 countdown timer...

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Static 20 countdown timer...

Post 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);
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Static 20 countdown timer...

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Static 20 countdown timer...

Post 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?
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Static 20 countdown timer...

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Static 20 countdown timer...

Post 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).

Code: Select all

window.location = "/logout.php";
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Static 20 countdown timer...

Post by orbdrums »

Cool. It works and thanks for your help!
Post Reply