0

I am creating an online exam page for my project. I have a countdown timer, but it resets on page refresh. How can I make it not to reset? The timer is set by fetching time from db. I am using php mysql. Please help me. Here is my code.

<?php
   $result=mysql_query("select * from test where testid='29'");
   while($time=mysql_fetch_array($result)){
      $dateFormat  = "d F Y -- g:i a";
      $targetDate  = time() + ($time['duration']*60);
      $actualDate  = time();
      $secondsDiff = $targetDate - $actualDate;
      $remainingDay      = floor($secondsDiff/60/60/24);
      $remainingHour     = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
      $remainingMinutes  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
      $remainingSeconds  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
      $actualDateDisplay = date($dateFormat,$actualDate);
      $targetDateDisplay = date($dateFormat,$targetDate);
   }
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Untitled Document</title>
   <script type="text/javascript">
      var days = <?php echo $remainingDay; ?>  
      var hours = <?php echo $remainingHour; ?>  
      var minutes = <?php echo $remainingMinutes; ?>  
      var seconds = <?php echo $remainingSeconds; ?> 
      function setCountDown ()
      {
          seconds--;
          if (seconds < 0){
             minutes--;
             seconds = 59
          }
          if (minutes < 0){
              hours--;
              minutes = 59
          }
          if (hours < 0){
              hours = 23
          }
          document.getElementById("remain").innerHTML = "  "+hours+" hr "+minutes+" min    "+seconds+" sec";
          SD=window.setTimeout( "setCountDown()", 1000 );
          if (minutes == '00' && seconds == '00') { 
              seconds = "00"; window.clearTimeout(SD);
              window.location = "result.php"
          } 

       }
    </script>

</head>
<body onLoad="setCountDown();">
   <div id="remain">
         <?php echo "$remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?>
   </div>
Shaunak Shukla
  • 2,347
  • 2
  • 18
  • 29
user1492669
  • 41
  • 1
  • 2
  • 10

4 Answers4

2

You should read the value into session variable and using that afterwards:

<?php
session_start();
if (isset($_SESSION['targetdate'])) {
    // session variable_exists, use that
    $targetDate = $_SESSION['targetdate'];
} else {
    // No session variable, red from mysql
    $result=mysql_query("select * from test where testid='29' LIMIT 1");
    $time=mysql_fetch_array($result);
    $dateFormat = "d F Y -- g:i a";
    $targetDate = time() + ($time['duration']*60);
    $_SESSION['targetdate'] = $targetDate;
}

$actualDate = time();
$secondsDiff = $targetDate - $actualDate;
$remainingDay     = floor($secondsDiff/60/60/24);
$remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-         ($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-    ($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);

?>
Kristian
  • 3,283
  • 3
  • 28
  • 52
  • Soory, there was a typo. I had "$targetdate" instead of "$targetDate", try now – Kristian Jun 30 '12 at 07:24
  • How to do the above for a logged in user in cakephp 2.4.6 ? Because once after login the session has already started and we can not start the session again for the countdown timer.As session starts only once.we can not destry the session to start it again for count down timer. – Ipsita Rout Mar 28 '14 at 07:14
  • If you know session is already started, then just get rid of `session_start()` from my code. If session isn't always started, then you can add if conditon to check if session is started. Like so: http://stackoverflow.com/a/18542272/1119550 – Kristian Mar 28 '14 at 07:42
1

When the user starts the exam, save the start time in the database. Use that for everything. Don't rely on client side code for this as the user can easily modify it.

If you save the start time, you know when the user started and what the current time is. You can use that to see if time is up or not. A javascript timer is good to show how much time is remaining, but it should be getting that info from the database.

sachleen
  • 30,730
  • 8
  • 78
  • 73
1

You'll probably need to register a table like active_exams with an examId field and datetimeStarted. Then load the timer as the number of seconds for that exam since it was started.

You may be able to create some workaround with cookies, localStorage or even the session but keep in mind those will be modifiable (or at least forgettable in the case of the session) by the user.

Bob Davies
  • 2,229
  • 1
  • 19
  • 28
  • thanks. but i dont no how to implement it. I am new to programming. Ca u help me with this. – user1492669 Jun 30 '12 at 07:19
  • Without access to your specific dev environment not really. Simply create a table in your database to hold the active_exams and plug away at it until you can generate an ID and retrieve the dateTime difference out to the page. It's not complicated and should be buildable even by a beginner within an hour or so. – Bob Davies Jun 30 '12 at 07:22
1

Page refresh doesn't matter if you have the Timer's "start-time" saved in your database for particular user.

Fetch this "start-time" from database and compute it to get the consumed time and accordingly show the remaining or elapsed time.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Rajiv Bhardwaj
  • 293
  • 3
  • 9