views:

24

answers:

3

In other words, what technology would take care of doing the time tracking? Would it be the JavaScript? I don't see being able to keep track of such things with PHP.

Here's what I need to accomplish:

  • I need to have a long form spanning many web page reloads because it is pretty much an online test where each page load displays a new question. However, the entire form, which constitutes one test has a time limit. When the time expires if the user in question has not completed the test then he/she cannot submit a partially completed test nor attempt to do the entire test all over again within the same day (either calendar date or with 12/24 hours in between.) The user may, however, comeback the next day and attempt to finish the entire test again within the allotted time. I know I have added a lot of details and I did this just to show context, nevertheless, the main difficulty for me would be in how accomplish the time expiry feature. That is, somehow, within a series of pages that make up a form representing an online test I want to track the time starting from the first question (one page load) and upon time expiry for the test to be disabled.

Has anyone ever done this? Would anyone have any tips for me on how to accomplish this? Any advice I can get would totally be appreciated in advance.

+1  A: 

If you do track time on client-side - always validate it on the server-side.

Never trust the client, by itself, to validate the time. As mentioned in the comments, client-side time validation is only good for cosmetic features, never for actual validation.

The easiest way to accomplish this is to add a unique token to the form (which is not spoofable) on first navigation. Cookies, or any other sort of session management technique you get from your framework will suffice.

On form submission you can first validate this on client side and return an error if time has passed, even before actually sending the form. If successful, submit the form, and make sure you validate the token upon processing on the server.

Yuval A
+1 this.No, if you wan't any validity on the server side, you would absolutely not do this in js on the clients browser, or than perhaps displaying time remaining or similar cosmetic functions. Your easiest solution would probably be an encrypted cookie with a date value of the most recent visit that goes against a database similar to timestarted/mostrecentvisit/userid
Serapth
A: 

There are two ways you could measure the "time they spent" on the form.

  1. When the first page of the form is severed, in the PHP create a session variable containing the date. When they finish the form, you subtract the current date form the beginning date. This gives you the total time it took from when the form was served and finished. However, this is not 100% accurate as there could downtime for other reasons such as slow internet.
  2. You could have JavaScript record the time on that page (I answered how to do that here: here) Using AJAX, this time could be sent that way or by using GET posts. The time would then be kept by PHP somehow and added up in the end.

Hope this helps! Just ask if you want an example.

PlagueEditor
Re: 2 - can be easily spoofed.
Yuval A
Yes, but I thought I should suggest it nonetheless given it shows a Javascript method. But you are correct; I recommend option 1.
PlagueEditor
A: 

In the most general terms, you'll need to set up a session on the server side to track each user and test. When the user begins the test, you stamp a variable (server side) with the test start time. As the user progresses through the test and requests additional pages, check whether the difference between the current time and that variable have exceeded the time allowed for the test. If the test has expired, instead of delivering the next test page, you can close up the test and deliver a "time's up" page to the user.

I don't know what server-side environment you're using, but it almost certainly has some sort of session management framework available. To reliably control the testing environment, you have to manage this from the server side.

Ken Redler