Solved

How to pass Session start time to javascript

Posted on 2008-10-22
5
347 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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

770 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