The PHP script

We have now completed everything we need to execute the flash movie. What is missing is the php script, whith which we can write and store the xml files on the server. The php file is pretty simple. So let's go through.

In the beginning we make sure that the name of the xml file and its contents are received, which is what the first lines are saying. If not then we terminate all the action.

<?PHP

//testing if var from flash are defined and have contents
if (!isset($fName)  || !isset($x_name)) {
	fail("Variables are empty.");
}

Next we need to eliminate the slashes (\) from the xml file, which are automatically created. If everything is fine we now execute a function to create or open a file.

//delete all slashes from the xml
$x_name = stripslashes($x_name);

//function to add or delete information
switch($action) {
    case "openfile":
        reopen($fName, $x_name);
        break;   
    default:
        fail("unknown action");
}

The fopen function does both. It will create a new file, if there is no file with that name or it will open a file with that name. We also allow 'w' to write into the file, which is done by the fwrite function. After closing we execute a function success, which signals back to the movie that there were no problems.

//function to add information
function reopen($fName, $x_name) {
    
    //existing file will be opened and non-existing will be created
    $file = fopen($fName, 'w');
    
    //if unable to create or open then fail
    if (!$file) {
        fail("File does not exist.");
    }
    
    //writing the replaced information
    fwrite($file, "$x_name");
    fclose($file);
    success();
    
}

//function in case of failure
function fail($errorMsg) {
	echo "&result=Fail&errorMsg=$errorMsg";
    exit;
}

//when it was successful...
function success() {
    echo "&result=Okay";
    exit;
}

?>

This brings us to the conclusion of this tutorial. What you need now is a login system which is combined whith the game or whatever and which will then replace the textinput for the username.

PREVIOUS PAGE

1      2      3      4