How to restore an old session when a user accidentally closes the browser?

I am planning to create an online sytem exam in PHP. What steps can I take to restore an old session if a user accidentally closes a window?

Suppose that he already answered 49 out of 50 questions and suddenly turned off the power (and there is no UPS), or he accidentally closes the window (even by mistake if he clicks β€œyes” on the javascript hint on the window.unload event), and then again opens the browser, everything is lost. What can I do to prevent this?

Thanks in advance:)

+4
source share
6 answers

Offer a login system in which you save the progress associated with the user’s login, or simply use cookies that do not expire when you close the browser (i.e. set an expiration date in the future).

+2
source

You will need to do one of two things:

  • Save the current state on the user's computer - this should be done through a cookie.

  • Save the current state on the server.

The second option is probably more reliable, it requires constant contact with the server. This will also allow the session to resume on another machine.

The first option is likely to be easier to implement.

+3
source

You can save session settings in a cookie that expires after 30 days, for example.

+2
source

You can save your state every time they move to the next question - if it is saved in a session, you can serialize the session and save it in the database associated with their account.

If they reopen the browser, you can download the saved session, unerialize it and continue where they left it.

+2
source

Saving the session identifier as a cookie with a long expiration time will solve the problem, but introduce a new problem: on public or shared computers, users will have to explicitly log out (that is, destroy the session), otherwise everyone who accesses the site after they log out will continue his session.

Another solution is to link the "exam sessions" to the user, save them in the database and continue the session if the user with the waiting exam is registered. Obviously, this requires a bit of coding :-)

+2
source
  • continuously save the state of the page in a cookie (for example, every time you change the form - while you have the state of the current page);
  • when sending, save the state in the session (or even to the database of incomplete forms) and clear it from the cookie (thus, you have the general state of the exam stored on the server side, so that you can delete it from the cookie).
  • When finished, clear both the cookie and the session.

Of course, if there is a power outage, the cookie may not have been flushed to disk yet, but otherwise (especially if you have several questions on one page), the user will lose less state than if you only saved on submit.

+1
source

Source: https://habr.com/ru/post/1314432/


All Articles