Jake
Jake

Reputation: 115

Get a server-safe timestamp in ActionScript 3

I would like to get the current Date/Time in actionscript 3, and I want to avoid the Date class since the system time can be easily manipulated by the user.

Can I access a server and get a timestamp?

Is there a better alternative to this method (a simple URL-request)?

Upvotes: 2

Views: 885

Answers (2)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Here is how you can do this:

The flash side:

var loader:URLLoader = new URLLoader()
loader.load(new URLRequest("yourserverpage.php"))
loader.addEventListener(Event.COMPLETE, onLoaded)
function onLoaded(evt:Event){
    var date:Date;
    try{
        date = new Date(Number(loader.data));
    }catch(e:Error){
    }

    if(!date){
        //flash couldn't convert the date properly, there was an error
    }
}

And the php side:

<?php 
    header("Content-Type: text/plain");
    print time() * 1000;
?>

Upvotes: 1

blue112
blue112

Reputation: 56542

Anything you can do in ActionScript can be manipulated by the user since it runs on its own computer, and you can't do anything about it.

That said, you can get the current timestamp from a server you own (for crossdomain reason). Just output a time() in PHP (for instance) and get it via an URLLoader.

Upvotes: 2

Related Questions