You have two options:
Save the progress when this happens (after each answer) and save the information about whether it was completed. This solution works even if the browser is closed (progress is saved in real time).
Use AJAX to call the server each for example. 5 seconds, and the server should return information about the end of the session or not. If the session has ended, then do what you need in JS (even redirect to another page). This solution does not guarantee the preservation of progress (the browser may be closed before the action is completed).
You can combine both options depending on what you need and how your application works.
If you reload the page after each answer (for example, reload to display the next question), you can use the option . 1 . If all questions are on the same page, select option no. 2 . If your application is a combination of the two, you can choose both.
EDIT:
Judging by your code, you are mixing:
- session in terms of answering questions using
- a session in terms of storing values between page requests in PHP .
The best idea is to separate them. Give the PHP session a much longer period because it is necessary for some other things, such as actually displaying progress or even saving progress. Instead, mark the start time of the test and save it in a PHP session. Thus, after the time for testing is exhausted, you can determine this and ignore any questions that were after that.
source share