Javascript countdown linked to php session?

Hi All,

Has anyone linked a javascript countdown script to the timeout associated with a php session?

I believe that 30 minutes is the default timeout for a php session. I’m interested to see if a javascript countdown can be displayed inside a members area to indicate that their session will expire in ‘x’ mins:secs if there is no activity on their part. It would be easy, I guess, to simply to reset a javascript countdown display script to 30 minutes each time the page was refreshed and/or a new page was visited within a logged-in area, but it would be better if the time to countdown from was intelligently passed from php to the javascript such that if, for example, the php session timeout was changed (from 30 minutes to 20 minutes) then countdown display would accurately reflect this.

Bottom line: this will only be useful if the countdown script is 100% accurately predicting the actual time when the session is due to expire so that users aren’t misled into thinking their session has yet to expire when in fact it has.

So if there are technical reasons (eg. php session expiry times are impossible to accurately predict/detect) why this can’t be done - I’d really appreciate your feedback!

Thanks.

What I did was register a TimOUT session variable. Then I did the following:



<script>
<?php
 $OUTTime = $TimeOUT + 1800; // calculate timeout from session. $TimeOUT was registered in the session
 $date = date("M d  Y H:i:s",$OUTTime);
?>
function getTime() { <!-- counting backwards and reLoad if session has expired -->
 now = new Date();   <!-- current Time -->
 later = new Date("<?=$date?>"); <!-- TimeOUT of session -->
 
 days = (later - now) / 1000 / 60 / 60 / 24;
 var daysRound = Math.floor(days);
 hours = (later - now) / 1000 / 60 / 60 - (24 * daysRound);
 var hoursRound = Math.floor(hours);
 
 minutes = (later - now) / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
 var minutesRound = Math.floor(minutes);
 if(minutesRound < 10){ minutesRound = "0" + minutesRound; } 
 
 seconds = (later - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound) - 1;
 var secondsRound = Math.round(seconds);
 if(secondsRound == 60){ secondsRound = "00"; }    <!-- Bug -->
 if(secondsRound < 10){ secondsRound = "0" + secondsRound; }  <!-- Bug -->
 if(secondsRound == "000"){ secondsRound = "00"; }   <!-- Bug -->
 if(secondsRound == "0"){ secondsRound = "00"; }   <!-- Bug -->
 if(secondsRound == "0-1"){ <!-- Bug -->
  secondsRound = "59"; 
  minutesRound = minutesRound - 1;
 }   
 
 var Time1 = minutesRound + ":" + secondsRound;
 if (Time1 == "00:00"){ <!-- RELOAD window if session-time has expired -->
  window.parent.location.reload(); 
 }
 
 window.parent.status = "Time to expire: " + minutesRound + ":" + secondsRound;
 newtime = window.setTimeout("getTime();", 1000);
}
</script>
</head>
<body onLoad="getTime()"> 

This works with me. On all the pages I have a check. In this script I check the time (is it within the 1800 seconds = 30 minutes)… If so: set TimeOUT to current time… If not: delete session and reload to login-screen

Because the getTime-function is on all pages, the page will be reloaded after 30 minutes. Because the session has been deleted… the window will be reloaded and the user will see the login-screen again.

Wow, thanks jazztie - that looks just the ticket. Will check it out…

I have a similar issue, but am a little confused.

<?php
$OUTTime
= $TimeOUT + 1800;
// calculate timeout from session. $TimeOUT was registered in the session
$date = date(“M d Y H:i:s”,$OUTTime
);
?>

Can you show the code used when creating the session. I don’t understand how $TimeOUT was registered

Thanks in advance,
Dave

Looking for a solution to this problem myself, though I was in no real hurry to solve it, you’ve saved me some work :slight_smile:

Thanks a lot Jazztie :slight_smile:

Not saying this is better, just what I did.


<script language="JavaScript">
<!-- Begin
function time_left() {
	left_minutes--;
	page_output = "Idle Logout in " + left_minutes + " Minutes";
	box_title_right.innerHTML = page_output;
	if (left_minutes == 5) {
		window.alert(page_output)
	}
	if (left_minutes > 0) {
		setTimeout("time_left()", 60000);
	}
}
var left_minutes = 59;
time_left();
//  End -->
</script>