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!
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".
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?
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.
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);
window.setTimeout(function() {
alert(/* some message about logging out in two minutes */);
window.setTimeout(function() {
// redirect
}, 2*60 * 1000);
}, 18*60 * 1000);
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.
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.