views:

80

answers:

6

Hello,

I am planning to create a online examination sytem in PHP. What steps could I take to restore 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 :)

+3  A: 

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).

deceze
Hi deceze, this data are of no use to store it in database. Can i do something with cookies? I know that session id is stored is cookies. Can i tweak something with that cookie? so that old session id is sent to web server and that way old session is restored?
Ankit Rathod
@Nitesh You're probably using the default values for session cookies, which means they're expiring upon the browser being closed. You can set different settings using `session_set_cookie_params` http://php.net/manual/en/function.session-set-cookie-params.php or via various ini settings http://www.php.net/manual/en/session.configuration.php
deceze
@Nitesh Panchal: Why not? We used a database table named like "unfinished_surveys" precisely for this, and regularly purged it for completed or abandoned entries. You don't have to use DB only for long-lived data.
Piskvor
+2  A: 

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

galambalazs
Hi galam, that's what i wanted to know. By which name is session id stored in cookie in PHP? Like in Java it is jsessionid. Likewise what name does PHP give?
Ankit Rathod
http://php.net/manual/en/function.session-id.php
galambalazs
+3  A: 

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.

ChrisF
+2  A: 

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.

xil3
+2  A: 

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 :-)

Iacopo
+1  A: 
  • 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.

Piskvor