Solved

How to pass Session start time to javascript

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now