Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to pass Session start time to javascript

Posted on 2008-10-22
5
Medium Priority
?
364 Views
Last Modified: 2008-11-04
Hello,
I wonder if you can help me?
I need to use the php session start time for timeout and timer functions by passing it onto an existing javascript code.

This is for an online test accessed after sucessful user log in.
I have copied the javascript which handles the timeout and coundown timer events.
What I need to know is if i can pass the $_session start time to the javascript and what would be the syntax for this?

Here is the script for the timer etc.
--------------------------------------------
<script type="text/javascript">
var minutes = 2;
var timeLeft = minutes*60*1000;
var tId = "";
var leave = "";
function countdown() {
       timeLeft -= 1000;

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

}
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" )
                  f.elements[i].disabled=true;

            }
            clearInterval(tId);
            clearTimeout();
            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)
            {
                  f.elements[i].disabled=false;
            }
      return true;
      }
</script>
----------------------------
0
Comment
Question by:Erikal
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 22781395
You can store the session start time in a cookie, and access that cookie via javascript.
Or right after the session starts, yo can echo time() in a script tag. i.e. :
<script type="text/javascript">
sessionStartTime = '<? echo time(); ?>';

Perhaps this link is usefull to : http://devzone.zend.com/node/view/id/646
0
 

Author Comment

by:Erikal
ID: 22781649
Thank you.
How can I adapt my javascript code above, so that timeLeft variable is calculated from session start time and time() and checks is 15 minutes elapsed or not.
in other words: Supposing that time allowed for the test is 15 minutes,
can I use-
timeLeft = present_time() - sessionstart_time
 if (timeLeft<=0){
      nextPage();
      } else {
      updateTimer();
   }

and what would be the correct synta here for passing php var into javascript?
0
 

Author Comment

by:Erikal
ID: 22781685
Sorry correction:
timeLeft = (sessionstart_time + 15*minutes) - present time()
if (timeLeft<=0){
      nextPage();
      } else {
      updateTimer();
   }
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 1500 total points
ID: 22790161
The equivalent for php time in javascript is Math.round(new Date().getTime()/1000).
The difference after subtracting is in seconds.
0
 

Author Comment

by:Erikal
ID: 22801814
I am sorry, but I can not quite follow this.
I am a beginner at this.
OK I echo the time() in javascript, but how do I set the session start time in php? or do I need to?
One more problem. if the user leaves the page without submitting and logs out, and comes back a second time loging in again (hence starting a new session) how will the time limit set for the test work?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question