Flash Labs > Lab 8: Logging User Data (via PHP)

Overview

In this lab you will learn how to log user behavior and data from your Flash-based UI prototype in to a text file. Since the Flash player cannot write to a file directly, you will accomplish this by having the SWF file talk to a PHP script.

Goals

Learn how you can use Flash to write information about what happened to an external text file. Become introduced to how PHP can complement Flash.

Example

This example will log the date/time of when it's been accessed by a user. View the log here. Note: If this example isn't running on a webserver with PHP support, the user data will not get logged. ...there is still a scary woosh sound when the palette opens.


Steps

Optional: Download the working files for this lab

When building UI prototypes it's common to want to log how long it took a user to perform a task, how many clicks it took, and even they're name and e-mail if they are using the prototype remotely. This required to prototype to create or open a file and write information to it.

Macromedia's official statement is disappointing to Flash users: For writing to files, use Director's File I/O Xtra. Do not use Flash as the Flash Player does not support full file input/output natively.

But what if you don't want to use Director? It's possible to do this with Flash if we get fancy and use a scripting language like Perl, ASP, Cold Fusion or PHP. Here's an example using PHP: a Flash-based online guestbook.

This lab will use PHP as well. In order to use PHP your machine/server needs to be configured for it. I will provide instruction for setting up OS X to handle this. If you don't have OS X, you'll need to either load PHP and Apache on your Windows machine or find a web server that's already configured for it.

Steps for setting up OS X as a web server:

    1. Turn on Web Sharing by going to System Preferences > Sharing and turning on Personal Web Sharing.
    2. You'll notice a Network Address on the top. Type this address in your web browser and you'll see the index.html file stored in the folder Home > Sites. If you've gotten this far, your computer is now a web server, but it doesn't support PHP... yet.

Steps for setting up OS X with PHP:

    Just download the PHP installer and run it. If you have problems, detailed instructions are provided here.

Creating the .PHP file:

    So now that your machine is configured, you'll need to create a PHP file that will write information to a text file.

    I've created the following PHP file--saved as writetofile.php--that will take the variable "inputText" from your Flash movie and write the information in that variable in to a text file called "test.txt":

    < ?.php
    ### Retrieves the variable inputText from Flash

    $inputText = $_POST['inputText'];

    ### Appends inputText to file text.txt
    ### (Feel free to change test.txt to whatever you want)

    $handle = fopen("test.txt", "a");
    fwrite($handle, $inputText);
    fwrite($handle, "\n");
    fclose($handle);
    ? >

How to call the PHP file from Flash:

    From Flash, all you need to do is call the PHP file using a POST command, and the PHP file will be able to access the variables in the SWF file. Here is the line of code I used to call the PHP file:

    loadVariablesNum ("writetofile.php", 0, "POST");


Make sure when you check the files, you view them in a browser using the address of your machine, and not by referencing the file on disk.

For example, to test these SWF files on my Mac, I point my browser to http://127.0.0.1/~jmanzari/ and then navigate to the SWF file (127.0.0.1 means "this local machine" in IP address form).

Learn More

Macromedia's notes on Flash and PHP
Discussion group post on this topic