Integrating Flash with Web Applications > Sending and loading variables to and from a remote file
Sending and loading variables to and from a remote fileA Flash movie is a window for capturing and displaying information, much like an HTML page. Flash movies, unlike HTML pages, can stay loaded in the browser and continuously update with new information without having to refresh. You can use Flash actions and object methods to send information to and receive information from server-side scripts, text files, and XML files.
Server-side scripts can request specific information from a database and relay it back and forth between the database and a Flash movie. Server-side scripts can be written in many different languages: some of the most common are Perl, ASP (Microsoft Active Server Pages), and PHP.
Storing information in a database and retrieving it allows you to create dynamic and personalized content for your movie. For example, you could create a message board, personal profiles for users, or a shopping cart that remembers what a user has purchased so that it can determine the user's preferences.
You can use several ActionScript actions and object methods to pass information into and out of a movie. Each action and method uses a protocol to transfer information. Each also requires information to be formatted in a certain way.
The following actions use HTTP or HTTPS protocol to send information in URL encoded format: getURL
, loadVariables
, loadMovie
.
The following methods use HTTP or HTTPS protocol to send information as XML: XML.send
, XML.load
, XML.sendAndLoad
.
The following methods create and use a TCP/IP socket connection to send information as XML: XMLSocket.connect
, XMLSocket.send
.
When playing a Flash movie in a Web browser, you can load data into the movie only from a file that is on a server in the same subdomain. This prevents Flash movies from being able to download information from other people's servers.
To determine the subdomain of a URL consisting of one or two components, use the entire domain:
Domain | Subdomain |
---|---|
http://macromedia |
macromedia |
http://macromedia.com |
macromedia.com |
To determine the subdomain of a URL consisting of more than two components, remove the last level:
Domain | Subdomain |
---|---|
http://x.y.macromedia.com |
y.macromedia.com |
http://www.macromedia.com |
macromedia.com |
The following chart shows how the Flash Player determines whether or not to permit an HTTP request:
When you use the XMLSocket object to create a socket connection with a server, you must use a port numbered 1024 or higher. (Ports with lower numbers are commonly used for Telnet, FTP, the World Wide Web, or Finger.)
Flash relies on standard browser and HTTP and HTTPS security features. Essentially, Flash offers the same security that is available with standard HTML. You should follow the same rules that you follow when building secure HTML Web sites. For example, to support secure passwords in Flash, you need to establish your password authentication with a request to a Web server.
To create a password, use a text field to request a password from the user. Submit it to a sever in a loadVariables
action or in an XML.sendAndLoad
method using an HTTPS URL with the POST
method. The Web server can then verify whether the password is valid. This way, the password will never be available in the SWF file.
Each action and method that loads data into a movie (except XMLSocket.send
) is asynchronous; the results of the action are returned at an indeterminate time.
Before you can use loaded data in a movie, you must check to see if it has been loaded. For example, you can't load variables and manipulate the values of those variables in the same script. In the following script, you can't use the variable lastFrameVisited
until you're sure the variable has loaded from the file myData.txt
:
loadVariables("myData.txt", 0); gotoAndPlay(lastFrameVisited);
Each action and method has a specific technique you can use to check data it has loaded. If you use the loadVariables
or loadMovie
actions you can load information into a movie clip target and use the data
event of the onClipEvent
action to execute a script. If you use the loadVariables
action to load the data, the onClipEvent(data)
action executes when the last variable is loaded. If you use the loadMovie
action to load the data, the onClipEvent(data)
action executes each time a fragment of the movie is streamed into the Flash Player.
For example, the following button action loads the variables from the file myData.txt
into the movie clip loadTargetMC
:
on(release){ loadVariables("myData.txt", _root.loadTargetMC); }
An action assigned to the loadTargetMC
instance uses the variable lastFrameVisited
, which is loaded from the file myData.txt
. The following action will execute only after all the variables, including lastFrameVisited
, are loaded:
onClipEvent(data) { goToAndPlay(lastFrameVisited); }
If you use the XML.load
and XMLSocket.connect
methods, you can define a handler that will process the data when it arrives. A handler is a property of the XML or XMLSocket object to which you assign a function that you have defined. The handlers are called automatically when the information is received. For the XML object, use XML.onLoad
. For the XMLSocket object, use XMLSocket.onConnect
.
For more information, see Using the XML object and Using the XMLSocket object.