In this tutorial I will show you how to send som data from flash to PHP, encrypting it and sending the answer back again. If you just want the code and no explanation jump to the last page...
Part 1. Flash
I'm just going to get started, so first get create a new flash document, than open the actionspanel. Then lets start coding:
var send:LoadVars = new LoadVars;
var receive:LoadVars = new LoadVars;This simply creates two loadvars variables called send and receive. We use loadvars to send data from flash to PHP (or any other server-script) and geting the answer back.
than we need to give the send loadvars som data to send. To do this we use
loadVars.nameOfVariable = valueOfVariable;Here you need to replace the loadVars with the loadVars (in this case send), the nameOfVariable with a variablename (could be nearly anything, but for this case lets use toPHP) and the valueOfVariable with the value of a the variable or the data to send in other words... so lets type
send.toPHP = "test";Good, the next thing to be done is to tell what to do when flash receives the answer, or in other words when the receive loads the content of a site. To do so we use
loadVars.onLoad = function(){
//Here goes the code to execute when it loads
}Here too we need to change out the loadVars with a loadvars, but however, we must not replace the function() whit somthing... This is just the syntax of actionscript...
So what we want to do is to show the result of what the PHP script has sent... In the PHP part we're going to use toFlash as variablename when we send it back to flash, and to get that in flash we use:
receive.onLoad = function(){
encrypted = this.toFlash;
trace(encrypted);
}(The trace only outputs somthing, that means that the answer is putputed in the output-panel)...
Than we only need to tel flash to load the server script... To do so we use:
loadVars.sendAndLoad("serverScript",anotherLoadVars,"method");replase the loadVars with the sender and the anotherLoadVars with the receiver. The serverScript is simply a referance to the server script file... The method is eather get or post... I normaly prefer post (actually I've never used get, I don't even know if it works :p )... That would mean that ouer code would be like this:
send.sendAndLoad("encrypt.php",receive,"POST");Part 2. PHP
From here on I'm just going to code all in PHP commenting everything whitch can be usefull to know...
<?php
/*Getting the variable 'toPHP' sent from flash using the 'POST' method*/
$fromFlash = $_POST['toPHP'];
/*Encrypting the $fromFlash and storing it as $encrypted*/
$encrypted = md5($fromFlash);
/*getting ready to send a variable to flash named toFlash using '&variableName='*/
$toFlash = "&toFlash=";
/*Asigning the encrypted variable to $toFlash AFTER the '='*/
$toFlash .= $encrypted;
/*echoing out $toFlash so that flash can read it*/
echo $toFlash;
?>And that's it! Wasn't that easy?
Part 3. The code whitout coments
For those of you who are laisy (like me :p ) here goes the whole code whitout coments...
For flash:
var send:LoadVars = new LoadVars;
var receive:LoadVars = new LoadVars;
send.toPHP = "test";
receive.onLoad = function(){
encrypted = this.toFlash;
trace(encrypted);
}
send.sendAndLoad("encrypt.php",receive,"POST");And for the PHP :
<?php
$fromFlash = $_POST['toPHP'];
$encrypted = md5($fromFlash);
$toFlash = "&toFlash=";
$toFlash .= $encrypted;
echo $toFlash;
?>