How to calculate time elapsed between two consecutive requests?
I am want to develop a quiz site using MVC 2. A single page will display only one question. The examinee will select or type the answer and press Next button to proceed.
My business logic on the server side will record the time elapsed 开发者_开发知识库between two consecutive page requests. These time stamps must not be tampered with by any means to make a reliable diagnostic report.
What is the best way to handle this?
Well, I wouldn't actually record the time elapsed - I would record the time itself (in UTC). You can very easily go from that to a time between pages, but you can't go the other way. You may well find it's useful to be able to examine particular sessions later on, filtering by when they occurred (e.g. if someone claims to have had a problem at a particular time, you can check whether they were actually on the system).
All you need is some sort of session/user identifier to keep track of the user's session, and you can just record the current time. The timing will be as tamper-proof as the server system clock and the database itself.
You could use Session state on the server. However, you'll have to think through what happens if the user opens multiple tabs/browsers etc.
Alternatively, you could send the encoded timestamps to the client. Since they are encoded clients will not be able to modify them.
Record the time and store in Session. So there's no way for any means of Client data tampering.
精彩评论