-1

I have a quiz appearing website where a user can appear for quiz. It has a feature of time out and auto submitting of quiz if time exceeds 30 minutes. Now the user who attempts the quiz is trying to stop the page processing when time-out occurs and stops the page loading by clicking the X button in browser. How can this be prevented. I have seen results for disabling refresh button and all but this is not my case. The user is stopping the page load when auto submit is being done and the result is being processed. Can I have some insights on this.

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0'; 
if($pageWasRefreshed ) {
  echo "<script>window.location.href='dashboard.php'</script>";die;
}

the above sent code is not of use.

Ansh
  • 269
  • 2
  • 15
  • 7
    You cannot stop fundamental functions of a browser like refresh or stopping page load. What you need to do is devise a system that works *despite* of anything the user may do client side. Which mostly means a lot of state keeping and validation **on the server**. – deceze Aug 22 '18 at 11:15
  • 1
    I sure hope that is never possible. I fear ads will be popups that can't be closed – Andreas Aug 22 '18 at 11:17
  • You can't prevent user from closing the window but you can warn using the onbeforeunload event https://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own – Timo Aug 22 '18 at 11:21
  • At most you can just add a `confirmation popup` using `onbeforeunload` but you can't restrict user to close browser window. – Vikasdeep Singh Aug 22 '18 at 11:21
  • Its not about closing the window. Whenever a request is made on browser to a page the browser loads the page and during that time the stop loading the page is clicked. – Ansh Aug 22 '18 at 11:23
  • 1
    So, how would you conceptually think it possible to send something to the browser with your page that would prevent the browser from cancelling your page to load? Like… what if I cancel right in the middle of that *something* being loaded…? No, you're barking up the wrong tree. – deceze Aug 22 '18 at 11:26
  • Why a down-vote? Can someone explain – Ansh Aug 22 '18 at 11:34

3 Answers3

0

You can try to use Ajax to preventing the user from stop your request.

If the time exceds 30 minutes, send a request to your server "underground" submitting the user's form.

You can use JQuery Ajax or any other that you like.

-1
<script language="JavaScript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        confirm( "You have attempted to leave this page. Are you sure?" );
    }
</script>

You can use onbeforeunload

Dieter Kräutl
  • 657
  • 4
  • 8
  • which page i should put this code on. Actually my page for displaying Quiz and processing quiz are different. `quiz.php` and `processquiz.php` are two pages. – Ansh Aug 22 '18 at 11:30
  • display page. Thats the one you want to protect, right? – Dieter Kräutl Aug 22 '18 at 11:31
  • See , the display page auto submits on time lapse. So when posting the values from quiz.php to processquiz.php the user is interrupting. – Ansh Aug 22 '18 at 11:33
  • You need that code on the page that you want to protect. both of them if nessessary – Dieter Kräutl Aug 22 '18 at 11:34
-2

Just a suggestion - why don't you use an alert on that time-out, to inform the user what you are doing and that can they please wait? then at least they have to close that window before continuing.

setTimeout(function(){ alert(" Your quiz is currently being auto-saved, please do not close your window."); }, 18000000);

Just cater the timeout to fit your own.

Sparlarva
  • 790
  • 1
  • 8
  • 30