How to Check Authenticity of an AJAX Request

后端 未结 15 873
悲&欢浪女
悲&欢浪女 2021-01-31 08:47

I am designing a web site in which users solve puzzles as quickly as they can. JavaScript is used to time each puzzle, and the number of milliseconds is sent to the server via A

15条回答
  •  抹茶落季
    2021-01-31 09:27

    You have to use server-side time here. Here is how I would do it:

    Make an AJAX request on document ready to ping the server. When server-side code receives the ping, store the server-side time as a session variable (making sure the variable does not already exist). When they finish the quiz, take the server-side time again and compare it with the session variable to determine their duration. Remove the session variable.

    Why this works:

    • You do not start the timer before they see the quiz
    • The network delay is factored in, because the timer does not start until the AJAX request comes in (if they have a slow connection, the AJAX request will be slow)
    • Ping is not spoofable because you make sure the session variable does not exist before storing

    EDIT: I wanted to add that you could continue to keep client-side time, and include it in the final post. Then you can compare it with your server-side calculated time. If they are reasonably close, then you can trust the client time.

提交回复
热议问题