Solved

How to pass Session start time to javascript

Posted on 2008-10-22
5
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…

733 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