Jump to content

Problem With Session Time Out


divyac
 Share

Recommended Posts

i am doing a quiz website in which i have 4 different category of questions and for each category,there should be a time limit.for that i tried out the following session time out code.

if(!session_is_registered("session_count")) {	  $_SESSION['session_count']=0;	  $_SESSION['session_start']=time();}$session_timeout = 20; // in sec$session_duration = time() - $_SESSION['session_start'];if ($session_duration > $session_timeout) {	  header("Location: timeout.php");  }$_SESSION['session_start']= time();$_SESSION['session_count']++;

if the time reaches 20 seconds, the page should be redirected to timeout.php.If i use the above code,the page gets redirected to timeout.php but only if the user is inactive..but my requirement is that while answering itself,the page should be redirected to timeout.php after 20 seconds..pls help me.....

Edited by Divya_C
Link to comment
Share on other sites

PHP isn't an "interactive" language in that sense - you can't run a bit, then wait, then run a bit more (without AJAX), as the server will wait for the entire script to finish before sending the resulting document to the user - so your script will just make your page hang for 20 seconds then redirect. You may want to look into a JavaScript solution.

Link to comment
Share on other sites

PHP isn't an "interactive" language in that sense - you can't run a bit, then wait, then run a bit more (without AJAX), as the server will wait for the entire script to finish before sending the resulting document to the user - so your script will just make your page hang for 20 seconds then redirect. You may want to look into a JavaScript solution.
I have tried javascript also as follows:
<html><script type="text/javascript">  function display( notifier, str,a ) {	document.getElementById(notifier).innerHTML = str;  }	  function toMinuteAndSecond( x ) {	return Math.floor(x/60) + ":" + x%60;  }	  function setTimer( remain, actions ) {	(function countdown() {	   display("countdown", toMinuteAndSecond(remain));			   actions[remain] && actions[remain]();	   (remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);	   if(remain==0){		document.location.href='timeout.php';	   }	})();  } </script><body onLoad="setTimer(30, {	10: function () { display('notifier', 'Just 10 seconds to go'); },	 5: function () { display('notifier', '5 seconds left');		},	 0: function () { display('notifier', 'Time is up baby');	   }  });"><div id="countdown"></div><div id="notifier"></div></body></html>

But if i use this script in my project,in every question load,the page gets refreshed and the timer also gets refreshed..so i am facing problems.Any other solution??

Link to comment
Share on other sites

The solution is not to refresh the page to get the next question. You can either show all of the questions on one page, or use an iframe to load the page and have the parent keep track of time, or use AJAX to swap out the question.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...