开发者

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

I am planning to create a online examination sytem in PHP. What steps could I take to restore开发者_如何学Go old session, if user has accidentally closed the window?

Suppose he has already answered 49 questions out of 50 and suddenly there is power cut off (and there is no UPS) or he accidentally closes the window (even by mistake, if he clicks yes on javascript's prompt on window.unload event) and then reopens the browser, everything is lost. What could I possibly do to prevent this?

Thanks in advance :)


You would need to do one of two things:

  1. Persist the current state on the user machine - this would have to be done via a cookie.

  2. Persist the current state on the server.

The second option is probably more reliable, it does require that you are in constant contact with the server. It would also allow the session to resume on another machine.

The first option would probably be easier to implement.


Offer a login system where you store the progress tied to the user login, or simply use cookies that do not expire upon closing the browser (i.e. set an expiry date far in the future).


You can store the session parameters in cookie which expires after 30 day e.g.


You could save their state every time they go to the next question - if it's saved in a session, you could serialize the session and save it in the DB, related to their account.

If they open up the browser again, you can then load up the saved session, unserialize it, and continue off where they left it.


Storing the session id as a cookie with a long expire time will solve the problem but will introduce a new issue: on public or shared machines, users will have to explicitly log out (i.e. destroy the session) otherwise everyone that access the site after they quit will continue their session.

Another solution is to bind 'exam sessions' to the user, persist them on the database and continue the session if an user with a pending exam logs in. Obviously this require a bit of coding :-)


  • continuously save the page state into a cookie (e.g. on every form change - thus you have the state of the current page);
  • on submit, save the state into session (or even into database of unfinished forms) and clear it from cookie (thus you have the overall state of the exam stored server-side, so you can clear it from the cookie).
  • When finished, clear both cookie and session.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜