我正在创建一个在线考试..我得到了我的计时器30分钟它将自动提交考试但每次刷新或重新加载时间恢复30分钟是否有任何方法来预防它?提前谢谢
<?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>
我只是复制这段代码我想限制测验计时器只有30分钟,什么是$ _SESSION ['targetdate'] ??
答案 0 :(得分:0)
不,这是不可能的。 PHP是一种服务器端语言,对客户端没有任何影响。
即使有可能,这样做只会让用户烦恼。当他们按下刷新按钮时,他们希望页面刷新
答案 1 :(得分:0)
您可以通过onbeforeunload事件监听器https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload
在浏览器中捕获更新