Tutorial details:
Written by: Jeffrey F. Hill
Time: 30 minutes
Difficulty Level: Intermediate
Requirements: Flash 5, CGI Server Support.
Topics Covered: How to load and edit server-side variables via flash.

Download the source for this tutorial here. (Zipped, PC file).

Introduction

The purpose of this tutorial is to not only load variables from a text file into your Flash movie, but also be able to edit and update those variables from that same movie or from a secure administrative area where only you have access.  The only required knowledge for this tutorial is a basic understanding of using actionscript in Flash and access to a server that allows you to run cgi scripts.  Other then that it's just changing a couple variables, cut and pasting, and uploading 2 files.  The basic concept behind this tutorial will make it possible for you to add any number of new interactive features to your website with just a few changes in the code. Here is the online example so you can follow along:  http://www.snowvids.com/PerlTut/PerlWriteVarsToText.html

Included Files:

  • UpdateVars.cgi (Perl Script used in example)
  • PerlWriteVarstoText.fla (Source Flash File for Example)
  • Ex2TextFile.txt (Can be named anything as long as you change the urls and paths to it)
  • subparseform.lib (No changes need to be made to this file - It just contains a subroutine for parsing the data from the Flash Movie)

Part I - Setting up the Perl script

The hardest part of using a cgi script is the setup and getting all your paths correctly.  I'll attempt to explain all this in as much detail as I can.  Along with some common errors that you can make, and how to avoid them.  First you'll have to change a couple things in the actual Perl script. You should only have to change Line 15 and possibly Line 1.  (for example purposes line numbers were included below- In the actual script these should not be included): 

The script (That's it! not so bad huh):

1)  #!/usr/bin/perl
2)
3)  require "subparseform.lib";
4)  &Parse_Form;
5)
6)  $Title = $formdata{'Title'};
7)  $Contact = $formdata{'Contact'};
8)  $About = $formdata{'About'};
9)  $News = $formdata{'News'};
10) $Products = $formdata{'Products'};
11) $Link = $formdata{'Link'};
12)
13) @New =  ("Title=$Title&Contact=$Contact&About=$About&
Products=$Products&News=$News&Link=$Link");
14)
15) open (LOG, ">/usr/home/userName/public_html/PerlTut/Ex2TextFile.txt")
 || &ErrorMessage;
16) print LOG "@New";
17) close (LOG);
18)
19) print "Content-type: text/html  ";
20) print "Status=Success - Your Comments have beed updated. Please
 return to the main area to see the results";
21)
22) sub ErrorMessage {
23) print "Content-type: text/html  ";
24) print "Status=Connection Failed Please Check the path to the text File";
25) exit; }

Open up the attached Perl Script called "UpdateVars.cgi" with notepad or any other text editor.  

Line 1 - The first line contained in the script is called the shebang line.  You will notice that it has a # symbol followed by a ! symbol.  All other occurrences of the # symbol indicates a comment in a perl script.  The first line is a special case though.  It indicates the Path to a program on the server which can execute the script - Do not indent this or put it anywhere else other then the first line.  The program in most cases is something like Perl.exe.  Don't worry about that part though. Just make sure that the path is correct.  The most common path to perl is #!/usr/bin/perl On different types and different setups of servers it may be different however.  If your using an online host they should have the path to perl listed in their online documentation.  Change this line to reflect that path.

Line 3 - of the code includes the library file "subparseform.lib" into the context of the script.  All that is listed in this file is one subroutine that parses the incoming data.  You will never have to make any changes to the subparseform.lib file - It will work for any variable you can think of.  

Line 4 - Just includes a subroutine from the "subparseform.lib" file.  There is only one subroutine in this file. You can include as many others though, and use them in the script in the same fashion.  As your scripts become more complex this is an easy way to keep track of everything.  

Lines 6 to 11 - This just puts the incoming variables from the Flash movie into a form that you can work with.  It uses a function from the included "Parse_Form" subroutine.  Don't worry about this part it works - If you want more or less variables just use the same syntax and it'll work.

Line 13 -  This collects the variables and enters them into an array.  The most important thing to realize is the format their in.  They have to be listed as - FlashVariableName1=Value1&FlashVariableName2=Value2 etc. Flash can only read variables in from a text file when their in this format.  For this example it is really not necessary to enter the variable string into an Array.  You can accomplish the same thing later in the script by just printing the string to the log.  The only advantage of putting them into an array is that if the script gets more complex later on this is an easier method.

Line 15 - Opens up a log File for writing to.  This is indicated by the > symbol (If you just want to read from the log file use the < symbol or if you want to append to the data already in the text file use the >> symbol).  The path contained on this line is the most important thing you'll have to change.  This has to be the absolute path to the text file you wish to load the variables from in Flash and also to edit/update.  The easiest way to find the absolute path to the file on your server is to look at the path displayed when you use an ftp program like ws_ftp or CuteFTP and ftp to the directory in where you text file is located.

Line 16 - Prints out the variable string entered in the array to the text file.  This writes over the existing data. Basically after you've opened up the log file (in this case the text file) for writing it just prints out the new data over the existing.

Line 17 - Closes the Log.

Lines 19 and 20 - Prints out a success message which is read by the Flash movie when the script is done executing.

Lines 22 to 25 - This is just an error message that you will get if the script was unable to open the Text file for writing to.  Just an easy way to check to see if the scripts working.