Server-Side Applications

I l @ ve RuBoard

It is not the purpose of this section, hour , or book to teach server-side programming. In fact, dozens, and perhaps hundreds, of books teach only that one subject. But I will try to give you the basics to further demonstrate the LoadVars object.

Server-side programs, also called CGI (Common Gateway Interface) programs, can be written in a variety of languages. The simplest is Perl, which is what we will use here. You can also write them using C, C++, PHP, JSP, ASP, and just about anything else that your server supports.

Perl is a simple language that is installed on most Web servers. It is the choice of beginning CGI programmers and of those who don't have full access to their server, such as people who use a separate Internet service provider for their Web hosting.

Here is a basic Perl program; it outputs the words "Hello World!" when called:

 #!/usr/local/bin/perl print "Content-type: text/html\n\n"; print "Hello World!"; 

The first line of the program simply defines it as a Perl program. It tells the Web server to use the program /usr/local/bin/perl to interpret and run the code. This is the typical location of Perl on a typical Web server.

The other lines of the program use the simple print command to output text. The first line defines the type of document, in this case a text/html document. Web browsers use this first line to figure out how to interpret the rest of the document. The last line just outputs the "Hello World!".

If you put this code into a file named and upload this file to your Web server, you should be able to go to that location with a Web browser and get the words "Hello World!" back. For instance, if the file was uploaded to, you can go to that URL with Internet Explorer or Netscape to run the program.

Try it. If this is your first time using CGI programs, you will probably not see "Hello World!" but instead something like this:

 Forbidden You don't have permission to access /test/ on this server. 

Don't have permission! But it is your Web site!

Well, you have to set the permissions on this CGI program to allow it to run as a program. Otherwise, the security measures on your server forbid it from executing.

So how do you set the permissions of a file on your server? Well, this is where I stop. I can't tell you how to set the permissions on your server because there are many different types of servers, many different setups used by ISPs, and many different programs with different interfaces that allow you to set permissions. However, I will give you a list of things you can do to get your first CGI program up and running:

  • Figure out what type of Web site you have. Is it a company Web site? Then you probably have a company system administrator who can help you.

  • If you use a quality hosting provider, there are probably how-tos and help files that you can refer to on its Web site.

  • Quality Web hosting companies will also have customer support that may be able to help you.

  • If you are using a free or inexpensive Web hosting solution, you probably cannot run CGI programs at all.

  • Seek out a friend who has created CGI programs before. Tell her that you need to get your first Perl program up and running. Someone who knows Web servers can figure out your hosting situation and show you how to set up a Perl program in no time.

  • Partner with someone at your company who can write CGI programs. At most companies, Flash and CGI programming are considered different skills and are done by different people. So if you are asked by your company to set up a program that requires CGI programming, but you have never done it before, you may want to inquire about having someone else help you with the task.

For the rest of this hour, I'll assume that you were able to get CGI programs running on your server. Remember that you will have to upload the Flash movie to the same server where the CGI program is located. In most cases, you will want the movie and the CGI program to be in the same folder.

Here is an example of a simple CGI program that takes the input from an HTML form or a Flash movie and echoes them back to the user :

 #!/usr/local/bin/perl print "Content-type: text/html\n\n"; print "<HTML><BODY>\n"; read (STDIN,$QUERY,$ENV{CONTENT_LENGTH}); @data = split('&',$QUERY); foreach $item (@data) {  ($prop,$val) = split('=',$item,2);  $val =~ s/\+/ /g;  $val =~ s/%([\da-f]{1,2})/pack(C,hex())/eig;  print "$prop: $val<BR>\n"; } print "</BODY></HTML>\n"; 

Because this is a book about ActionScript, not Perl, I will quickly summarize what this code does. It takes the contents of an HTML form or a Flash LoadVars object and looks at each property/value pair, sending it back as a simple text page. It converts the text to normal text rather than the escape-character equivalent, which uses + rather than spaces and things such as %20 . Both HTML forms and Flash convert the data to this format before submitting.

Here is the HTML that could be used to call this CGI program:

 <HTML><HEAD> <TITLE>  Echo Test  </TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <FORM ID="echoTest" NAME="echoTest" ACTION="echo.cgi" METHOD="POST"> NAME: <INPUT NAME="name" TYPE="text" SIZE="30"><BR> ID: <INPUT NAME="ID" TYPE="text" SIZE="10"><BR> <INPUT TYPE="submit" VALUE="ID"> </FORM> </BODY> </HTML> 

You can find the CGI script on the CD-ROM in the folder 18send. It is called echo.cgi. The sample HTML is called echotest.html.

Upload these both to your server, set the permissions on echo.cgi to what your server requires for CGI programs, and open the HTML page in your browser. You should be able to type a name and ID in the HTML form, click Submit, and get back a list of the values you entered.

Now let's do the same thing in Flash. The movie 18echotest.fla contains two simple input text fields linked to the variables userName and userID . There is also a Submit button. The only script is on the Submit button. It creates a LoadVars object, sets two properties, and sends it.

 on (release) {  mySendVars = new LoadVars(); = userName;  mySendVars.ID = userID;  mySendVars.send("echo.cgi","_self"); } 

The second parameter, _self , means that the entire HTML page with the Flash movie will be replaced by the result of the CGI program. This means that it will work just like a normal HTML form.

You can test this movie by uploading both the .swf and the .html files to your server, in the same location as the working CGI program. The result should be the same as if you used the HTML form.

I l @ ve RuBoard

Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272 © 2008-2017.
If you may any questions please contact us: