-1

Im creating a Online Examination.. i got my timer for 30 mins it will automatically submit the exam but every time the page is refresh or reloaded the times resumes at 30mins is there any way to preven it? thanks in advance

   <?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);

?>
<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>

I just copy this code i want to limit the quiz timer for only 30 mins and what is $_SESSION['targetdate']??

2 Answers2

0

No, it's not possible. PHP is a server side language and has no influence at all over the client.

Even if it was possible, doing so would only annoy users. When they press the refresh button they expect the page to refresh

GordonM
  • 31,179
  • 15
  • 87
  • 129
0

You can catch updating in browser by onbeforeunload event listener https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload

Vlad Nikitin
  • 1,929
  • 15
  • 17