Sending Info to the Server

[ LiB ]

Sending Info to the Server

Now that you know how to download information into your program, you can learn how to send information to the server. You can send your information to a CGI script that will then do something with it. In order for the CGI script to work, it must be installed somewhere that can run these scripts. You can install CGI scripts locally only if your computer is set up as a server.

I'll be using the following Perl script for the purposes of this chapter. I called the script saveData and listed the code in the following listing.

 #!/usr/local/bin/perl print "Content-type: text/html\n\n"; read (STDIN, $QUERY, $ENV{CONTENT_LENGTH}); open (FILE, ">data.txt"); print FILE "$QUERY"; close(FILE); print "Data saved!"; 

As I'm not going to teach you how to program in Perl, I'm going to be brief about what each line does.

The first line is called the "shebang" line. This tells the UNIX system where to find the Perl interpreter.


On my Web server, the interpreter is installed under that pathyours might be different.

The following line tells the browser what type of information it is about to send. In this case, I will be sending HTML data.

 print "Content-type: text/html\n\n"; 

Once I have this ready, I'm ready to read the key-value pairs from the system's environment variablesthey are stored with the GET method of transferring data.


The environment variables are in the encoded format we went through before. All we have to do now is set up a file and write the information into it.

 open (FILE, ">data.txt"); print FILE "$QUERY"; close(FILE); 

For confirmation purposes, I decided to output a one-liner to the HTML spit-back page.

 print "Data saved!"; 

Go ahead and install the saveData program on your server. Once you're ready, come back and I'll show you how to send data to this program that will create the text file that you need to read in at a later time.



I have saveData installed on my server, but you cannot use it because Flash won't let you send data to a script on another computer. This is why you have to set it up on your server along with your SWF file.

I have written up GDA_PROG13.4.fla to show you how easy it is to send information to this script. Just click the button and you're set.

Check out the following listing to see what's in that magical button.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // This program demos how to use // the LoadVars object to send // info to a CGI script on (release) {   // Create the object   webData = new LoadVars();   // Populate the object = "Lewis,Judy,Julie";   webData.score = "123,456,987";   // Send it to the CGI script and output it within this HTML page   webData.send("cgi-bin/saveData", "_self"); } 

First I created a new LoadVars object for when the user releases the button. I then created the properties in a format that I can later parse. I then used the send method to send the info to the CGI script.

 // Send it to the CGI script and output it within this HTML page   webData.send("cgi-bin/saveData", "_self"); 

The first parameter is the URL where the scripts are storedremember that it must be on the same computer as the SWF. The second parameter tells the browser where to send the CGI script's response. When _self is used, the current Flash page is replaced with the output of the script.

Because this information was sent, a data.txt file was written with the following information:


This information can now be parsed and loaded just like we did before.

[ LiB ]

Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta © 2008-2017.
If you may any questions please contact us: