Simple session based timer for online quize website in PHP

  <?php  session_start();  /* If leave the page or refresh it, then the timer will continue to count down until finished. */  // $minutes and $seconds are added together to get total time.  $minutes = 20; // enter minutes  $seconds = 0; // enter seconds  $time_limit = ($minutes * 60) + $seconds + 1; // converts total time into seconds  if(!isset($_SESSION["start_time"])){$_SESSION["start_time"] =                          mktime(date('G'),date('i'),date('s'),date('m'),date('d'),date('Y')) + $time_limit;}  // Add $time_limit (total time) to start time. And store into session variable.  ?>  <html>  <title>Online Quiz</title>  <head>  <style>  #txt {  border:2px solid red;  font-family:verdana;  font-size:16pt;  font-weight:bold;  background: #FECFC7;  width:80px;  text-align:center;  color:#000000;  }  </style>  </head>  <body >  <script>  var ct = setInterval("calculate_time()",100); // starts clock.  function calculate_time()  {  var end_time = "<?php echo $_SESSION["start_time"]; ?>"; // get end time from session variable (total       time in seconds).  var dt = new Date(); // Create date object.  var time_stamp = dt.getTime()/1000; // gets current minutes (converted to seconds).  var total_time = end_time - Math.round(time_stamp); // subtract current seconds from total seconds to get   seconds remaining.  var mins = Math.floor(total_time / 60); // extract minutes from seconds remaining.  var secs = total_time - (mins * 60); // extract remainder seconds if any.  if(secs < 10){secs = "0" + secs;} // check if seconds are less than 10 and add a 0 in front.  document.getElementById("txt").value = mins + ":" + secs; // display remaining minutes and seconds.  // check for end of time, stop clock and display message.  if(mins <= 0)  {  if(secs <= 0 || mins < 0)  {   clearInterval(ct);   document.getElementById("txt").value = "0:00";   alert("The time is up.");     }    }   }  </script>  <br><br>  <center>  <div style="background:wheat; height:70%; width: 70%;">  <br>  <div id="txtMin" align="right" style="top: 0;" value="1"><b>Time:&nbsp;</b><form> <input id="txt"             readonly> </input></form></div>  <b><h3>Question No : </h3> Who Invented JAVA Language ?  <br></br>  <form method="POST" action="submit/">  <fieldset>  <input type="radio" name="ans"> James Gosline </input><br><br>  <input type="radio" name="ans"> Shubham Takode </input><br><br>  <input type="radio" name="ans"> Me  </input><br><br>  <input type="radio" name="ans"> Denis Ritche </input><br><br>  </fieldset>  <br><br><input type="button" value="PREVIOUS" />  &nbsp;&nbsp;<input type="button" value="NEXT" />&nbsp;&nbsp;<input type="submit"                      value="SUBMIT" />  &nbsp;&nbsp;<input type="button" value="END TEST AND SHOW SCORE" />  </b>  </form>  </div>  <div style="position: absolute; left: 0; bottom: 0; right: 0; background:wheat; height:10%; width=:100%;">  <center>  Designed and Developed By: <br>Shubham Takode (http://35.176.198.126)<br>  </center>  </div>  </center>  </body>  </html>

 

Comments

Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+