Saad Khawaja Menu

Permalink:

Get Time from the Server

A common error that developers make is that they use the time on the device for various game play elements.

For e.g. A game that requires the user to wait 2 hours before making another element.

This can be easily hacked by changing the device date/time. So, in order to stop cheating, you must get time from the server and compare the current time with that.

 

The easiest solution would be to make a PHP page and print time in it.

Then use WWW in Unity to query the page and get time.

 

PHP

  1. <?php
  2. print date(“G.i:s”, time());
  3. ?>

UNITY

void Start () {
        StartCoroutine("getTime");
 
    }
 
    IEnumerator getTime()
    {
        WWW www = new WWW("http://www.saadkhawaja.com/gettime.php");
        yield return www;
 
        Debug.Log("Time on the server is now: " + www.text);
    }
  • nobody

    hi, can you make the php include the date also ?
    it mean so much for me,
    thank you

  • asd

    hi, im using this solution… btw i’ve made my script to load on my server with apache, but unfortunately it require a user\pass to get the page load… if I put my page in CS script i got 401 error authentication required… do you know anyway to bypass that?

  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.