0

Is there a way to determine the time without using the system clock? When using javascript's Date, a user could simply change the time on his system clock in order to mess up your program. I would assume that checking the time from a reliable source such as Google automatically would be the way to go, but I don't know how to do that.

Edit: This is not a duplicate of the other question someone has referenced because this is Javascript, not Rails, and I cannot simply take the time from the host of the API.

  • YOu haven't specified what kind of application you are talking. lets say its a web application, then you should avoid user to post time those kind of field values should be generated at server side – ASP Jun 21 '15 at 22:15
  • 1
    If it's JavaScript, the user can modify it **always**. if you need a reliable time, do server side code. But if you need to pass it to the client at one time, the user again can modify it. – Reeno Jun 21 '15 at 22:15
  • Have you seen [this post](http://stackoverflow.com/questions/489581/getting-the-current-gmt-world-time/489846#489846) ? – Alp Jun 21 '15 at 22:16
  • 1
    The user is loading your page from your server. You can control the time on your server, can't you? (Or at least rely on it not to be changed arbitrarily by somebody.) – nnnnnn Jun 21 '15 at 22:16
  • @Reeno which is why I thought the solution would be to get the time from Google, a third-party source. –  Jun 21 '15 at 22:19
  • possible duplicate of [Free Rest API to get current time as string (timezone irrelevant)](http://stackoverflow.com/questions/13240530/free-rest-api-to-get-current-time-as-string-timezone-irrelevant) – xrisk Jun 21 '15 at 22:30
  • @DaemonOfTheWest I apologise for the incorrect answer. – xrisk Jun 21 '15 at 22:37
  • See example at bottom of http://www.timeapi.org – xrisk Jun 21 '15 at 22:38
  • @Rishav Kundu thanks, that worked. –  Jun 21 '15 at 22:39

1 Answers1

0

if you do not need the time to be accurate to the second you can pass your server time to the client application. do this by inserting a simple script with a var or similar to your html page.

than on load calculate the difference between the server time and the users time. with a long running client side application use this value at later points in time to calculate your the server time.

have in mind that the time value should only used in informal ways. a user can always alter a javascript application in the browser. if you need real validation you need to re-evaluate server-side.

dreamlab
  • 3,321
  • 20
  • 23