Recipe 17.1.
Creating and Opening a Local Shared Object
Problem
You want
to store information that persists between visits to a .swf
file.
Solution
Use a LSO.
Discussion
As mentioned in the introduction to this
chapter, LSOs are to Flash what cookies are to web browsersbut more
so. They are called "super
cookies" by some developers because they allow you to store large
amounts of data and store and retrieve many intrinsic ActionScript
datatypes (as well as objects created from custom classes). In
general, LSOs are referred to as Flash cookies.
The default maximum size that LSOs can grow to
is 100 KB. However, counting on the use of this much storage space
for a movie could lead to potential problems because users have
full control over the size of LSOs through the Flash Player's
Settings Manager and can restrict growth as they see fit. LSO files
are saved to the client computer in a binary file ending with a
.sol extension. Flash movies within the same domain can
write to and read from the .sol files by means of
ActionScript's flash.net.SharedObject class.
When a .sol file is created, it is placed
in an application data directory for the Flash Player. For users
using Microsoft Windows, it
will be in a directory something like this: C:\Documents and
Settings\[ username ]
\\Application Data\\Macromedia\\Flash
Player\\#SharedObjects\\ [ random character
directory name ]. On Mac OS X, the directory will
be something like /Users/[ username ]
/Library/Preferences/Macromedia/Flash
Player/#SharedObject/ [ random character
directory name ]. The random
character directory name is essential for
security purposes. A rogue .swf movie might try guessing the
name and location of a shared object from a particular web site so
that it can load the LSO from the filesystem to gain access to data
it normally doesn't have access to. After all, .sol files
are stored in a standard location. By augmenting the path with
randomness, guessing the path to the .sol files is virtually
impossible.
The static getLocal( ) method is the
mechanism by which LSOs are both created and opened for reading.
The method requires at least one parameter; a string that specifies
the name of the shared object to create or open:
var example:SharedObject = SharedObject.getLocal( "example" );
The getLocal( ) method attempts to first
locate an existing LSO by the specified name with a .sol
extension stored on the client computer. If none is found, the
Flash Player creates a new LSO with that name. In either case, the
existing or new LSO is opened. The getLocal( ) method
returns a SharedObject instance. It is used instead of the
new operator to instantiate a new local shared object.
See Also
Recipe
17.2 has important information on how to store data in a local
shared object. Recipe
17.3 has details on how to retrieve data from a local shared
object. Recipe
17.4 has information on the SharedObject.flush( )
method, which is used to manually save data to a shared object.
Recipe
17.5 discusses sharing data between movies. Recipe
17.6 has details on opening the Flash Player Settings dialog
box to the Local Storage tab to configure disk space usage.
|