Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jquery session timer

Status
Not open for further replies.
Aug 23, 2004
174
0
0
US
I am trying to create a session timer using jquery. I need to display an alert after 14 minutes, then if there is no response after 30 seconds log the user out and return to the login page. Here is the code I have so far. I can get the first alert to work, it's the second timer that's the problem.
Code:
			$("window").ready(function(){
					sessionTimer = window.setInterval("sessionAlert()", 10000);
			});

			function sessionAlert(){
				responseTimer = window.setInterval("end_session()", 5000);
				var answer = confirm("Your session is going to expire in 30 seconds, click ok to refresh")
				if (answer){
					<?php session_destroy()?>
					window.location = "/";
				}
			}
			function end_session()
			{
				<?php session_destroy()?>
				window.location = "/";
			}
 
I think what you're going to need to do is possibly use some sort of Ajaxy-type call in order to refresh / logout the user .... or attach a querystring variable to the window.location call.

In these javascript functions, the php session_destroy(); statement gets interpreted AND completed before any output to the browser screen. Therefore, your session is destroyed before you even hit the $("window").ready function call.

What you may want is something similar to the following:
Code:
$("window").ready(function(){
  sessionTimer = window.setInterval("sessionAlert()", 10000);
});

function sessionAlert(){
  responseTimer = window.setInterval("end_session()", 5000);
  var answer = confirm("Your session is going to expire in 30 seconds, click ok to refresh")
  if (answer){
    window.location = "/?destroy=false";
  }
}
function end_session() {
  window.location = "/?destroy=true";
}
and in the index page, at the top you have:
Code:
<?php
session_start() ;
if (isset($_GET['destroy'])) {
  switch($_GET['destroy']) {
    case "false" :
      // code to refresh session here
      // send back to referring page, if set
      $ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "index.php" ;           
      header("Location: $ref") ;
      exit ;
      break ;
    default:  // true or other values destroy session
      $_SESSION = array() ; // blank out session
      session_destroy() ;
      break ;
  }
} 
// more header code ...
Hope that helps.


Greg
 
Sweet, thanks a lot that works great. But now the issue is the second timer. 30 seconds after the first alert I need to automatically end the session, logout, and go back to the login page. Any ideas how to get that working?

Thanks again.
 
If anyone is interested here is the completed code. Instead of displaying a javascript popup I decided to show a hidden div with an ok and cancel button.

Code:
<script type="text/javascript"> 
	{literal}
		$("window").ready(function(){
			var sessionTimer = window.setTimeout("showSessionAlert()", 10000);
			$('.continue-session').click(function(){
				window.clearTimeout(responseTimer);
				hideSessionAlert();
			});
			$('.end-session').click(function(){
				endSession();
			});
		});

		function showSessionAlert(){
		        var responseTimer = window.setTimeout("endSession()", 5000);
			$('div#hidden-session-message').fadeTo("fast",.5);
			$('div#hidden-session-message').show();
			$('div#message-container').show();
			$('div#message-container').fadeTo("fast",1);
		}
		function endSession()
		{
			window.location = "/destroy";
		}
		function hideSessionAlert(){
			$('div#hidden-session-message').hide();
			$('div#message-container').hide();
		}
	{/literal}
</script>
 
LOL ... that's exactly what I was going to suggest ... I only was notified once on this thread instead of twice though ... hrmm ...

Oh well, glad it works for you.

Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top