session elapsed time

I have an online test with a set javascript time limit.
If the user submits the test they are not allowed back to the same page.
However, if the user leaves the page without submitting the test form, they can return anytime by loging in again.
What I need to do is to record the elapsed time at each session, so that I can restrict the total elapsed time for all sessions to a limit.
Say the user is allowed 1 hour for the test. In the first session they use up 20 minutes and in the next session 25 minutes. Finally they come back for the third time and when the remaining 15 minutes are up they are told the time is up.

Can someone please help me with the code on this.
So, can I for instance record the elapsed time to sql db and on the second session update the elapsed time by adding the elapsed time of the second session to the first one etc.

I am a beginner and your help with claer sample codes would be appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


I think when users take the test, you should store the start time in session. Then, when user leave the test (by clicking on "leave" link, for example), you can check current time to see how much time has passed. Then, you can add this time period to the total time your users had taken so far.
no sessions will not work for this.  A session is open for as long as the user FIRST visits a page and makes some selection that the session records.  If he comes back later and does NOTHING at all, the session timer is still ticking.  He can come back 20 times in 20 hours, do absolutely nothing -- or do something -- and the session timer CONTINUES to tick regardless.  Sessions will not work for this ACTIVITY BASED timer.

You will have to use cookies, because only in that way can you record the ACTIONS or else INACTIONS of the user.  It is going to be tricky exactly how to do this, if you want a solution, suggest which actions will trigger a "continue" mode, and which actions will trigger a "terminate" mode.
ErikalAuthor Commented:
Thank you scrathcyboy,
May be I can simplfy the problem.
If I set the initial number of minutes to 60 minutes (or 900,000 miliseconds) and record this in the db,   which is the time allowed for the test, and call it 'all_Time'. Then each time the student visits the page, I need to pass a variable (timeLeft) from the javascript to php and update the db with this.
In other words I do not need to deal with 'time()' in real terms, just the minutes and seconds remaining.
So, when the student comes back to the page next time, the updated minutes left in the db will become the new time limit.

Unfortunately, I am not quite sure how I should call these variables and how to pass them from javascript to php and back from php to javascript.

If I give you the script I am currently using, may be you can help me modify the script.
Here is the script for the whole time event of the test page: (I know I need to change
var timeLeft = minutes*60*1000;   to something like    var timeLeft = '<? echo $all_Time?>;':  to pass the sql var to javascript, but how and where in the script below do I pass the timeLeft to php so that the db can be updated.)

<script type="text/javascript">
var minutes = 2;
var timeLeft = minutes*60*1000;
var tId = "";
var leave = "";
function countdown() {
       timeLeft -= 1000;

      if (timeLeft<=0){
      } else {
window.onload=function() {
  //leave = setTimeout("nextPage()", timeLeft);
  tId = setInterval('countdown()',1000);

function updateTimer() {
   var timerArea = document.getElementById('timer');
   var numSeconds = timeLeft / 1000;
   var displaySeconds = numSeconds % 60;
   var displayMinutes = (numSeconds - displaySeconds) / 60;

   // format display a little more by putting a leading 0 for small seconds
   if (displaySeconds < 10) {
       displaySeconds = "0" + displaySeconds;

      timerArea.value = displayMinutes + ":" + displaySeconds;
      function nextPage(){
            //assuming you have <form id="English1"...>
            var f = document.getElementById("English1");
            for( var i=0; i < f.elements.length; ++i)
                    if( String(f.elements[i].type).toLowerCase() != "submit" )

            alert('Time allowed for the English test has expired! Please press the Maths Test button to continue');
       function activate(f){
  if (timeLeft > 100000 && confirm('Are you sure you want to leave the English test? You still have some time left to go back and check your answers. If you are sure you want to go on to the Maths Test, please click OK button. Otherwise, click Cancel button to return to the English test and use the remaining minutes to improve your English score.'))
return false;
            for( var i=0; i < f.elements.length; ++i)
      return true;
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

"Unfortunately, I am not quite sure how I should call these variables and how to pass them from javascript to php and back from php to javascript."

That's easy if you do it right.  You use a GLOBAL javascript variable, outside any function.  In the functions, you just USE the variable, not redeclare it, like this --


var timeleft = 6000;

your functions {

var getleft = timeleft;              // here you are not changing the global, just using it for a local var
 xxxxx                                          // cacluations
timeleft = getleft;            // after changing getleft time, you are now updating  the global

document.form1.time.value = timeleft;                  // put it into the input field before form submit

}   end your functions


FORM name="form1" method="GET" action="somefile.php">
<input type="hidden" name="time">

PHP ---

$timeleft = $ _GET["time"];

now it is in a php variable, manipulate it, and when you send back the response, put it back into the header --  HEAD -- <JAVASCRIPT>  var timeleft = <?php $timeleft ?>

Now it is written back to the head.  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ErikalAuthor Commented:
I am working on this.
Just one question:
The whole javascript I copied above is also in the HEAD section. Should I put the 'var timeLeft = <?php $timeLeft ?>' in the same script block on top, just before 'var timeLeft = 6000;'

Would the two 'var timeLeft' declerations conflict?
ErikalAuthor Commented:
another thing.
Can I actually put timeLeft in the db with the initial value of 6000 and update it with the new timeLeft value when the user leaves the page.
This way I would only have the 'var timeLeft = <?php $timeLeft ?>' in the javascript and can delete the 'var timeLeft = 6000;'

I need the timeLeft value updated in the db, so that everytimer the user leaves the page by refreshing or by navigating to another page, without actually using the 'submit' button of English1 form. When the user submits the English1 form the test is over and the timeLeft is no longer relevant.

Your suggestions are just what I am looking for, but how do I activate the sql update event when the user leaves the page without submitting the form.

I am sure this is quite simple, but I am such a beginner on this.
"but how do I activate the sql update event when the user leaves the page without submitting the form."

You can't,  a server script will have to get the value at some point to save it.  The only other way is to use a cookie on the users system, and if the kids are smart enough, they can get around it.  The code I gave you basically saves the timeleft in a variable, and it will have to go to the server and be saved for the student, in a server cookie or session, and then passed back to the student when they sign on again.

None of this is simple programming, and if you don't know javascript, or ASP or PHP, and sessions and cookie management, I doubt you will be able to pull it off without a lot of reading and working with the code
ErikalAuthor Commented:
Is there a way I can use an onUnload function to pass the timeLeft to server script.
I realy don't want to use cookies.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.