Advertisement
lalatino

session expire time countdown in javascript

Jul 17th, 2012
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php session_start();
  2. // http://www.daniweb.com/web-development/php/threads/124500/session-time-out-in-php
  3. // set timeout period in seconds
  4. $inactive = 600;
  5.  
  6. // check to see if $_SESSION['timeout'] is set
  7. if(isset($_SESSION['timeout']) ) {
  8.     $session_life = time() - $_SESSION['start'];
  9.     if($session_life > $inactive)
  10.         { session_destroy(); header("Location: logoutpage.php"); }
  11. }
  12. $_SESSION['timeout'] = time();
  13.  
  14. ?>
  15. <html>
  16. <head>
  17. <script type="text/javascript">
  18.  
  19. function start_onload(){
  20.     var expire_time = new Date().getTime() + 1000*<?php echo $inactive; ?>;
  21.     //console.log(expire_time);
  22.     countdown_session_timeout();
  23.     function countdown_session_timeout() {
  24.         var current_time = new Date().getTime();
  25.         var remaining = Math.floor((expire_time - current_time)/1000);
  26.         var timeout_message = document.getElementById('timeout_message');
  27.         if (remaining>0) {
  28.             timeout_message.innerHTML = 'Session will expire in '+ Math.floor(remaining/60) + ' min. ' + (remaining%60) + ' sec.';
  29.             setTimeout(countdown_session_timeout, 1000);
  30.         } else {
  31.             timeout_message.innerHTML = 'Session expired.';
  32.         }
  33.     }
  34. }
  35.  
  36. </script>
  37. </head>
  38. <body onload="start_onload()">
  39. <h1>Example of how can be set timeout done</h1>
  40. <p> http://stackoverflow.com/questions/11531299/html-session-timeout-in-progress-bar </p>
  41. <div id="timeout_message" style="position:absolute; left:0; bottom:0; background-color: #ddd; color: red;"></div>
  42. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement