Skip to Content

Home / Tutorials / Flash Cookies: Local Shared Objects

Flash Cookies: Local Shared Objects

by Philipp Kostin, Reader rating: 8.2

Philipp works as a freelance designer and developer producing Flash applications, games and web sites for various clients around the world.

He also owns FlashVista.com which showcases cool sites, resources, tutorials and games for Flash addicted people.

Before Flash MX it was pretty tricky to "remember" the data in a Flash movie. It could be done with a standard browser cookie (which can be hard to implement for someone with intermediate Flash skills), using a server-side script such as PHP or ASP or offline in Flash projector files with the undocumented fscommand "save". Flash MX's Shared objects allow you to store and retrieve the information within a Flash movie easily. Lets see how it works.

What is a local shared object?
Flash MX Shared objects are a new feature that allow you to store information on the clients machine the same way as cookies would and retrieve it at a later time. An example of a suitable application for Shared objects data could be to remember the user's name, the number of the level he last played in a game,his highscore or anything else you can imagine.

Shared objects are stored in .sol files located in the Flash player directory of the user's profile, example: "C:/Documents and
Settings/Administrator/ApplicationData/Macromedia/Flash Player", and have their own format.

Here is a working example of a movie using the Shared Object. Type in your name and age and click on "Save". Then, refresh this page to let Flash read and display the stored data:

Creating a shared object
First, we have to create a local shared object within a Flash movie. To do so,just put this line of code in the first frame of your movie:

local_data = SharedObject.getLocal("user_data");

Now we have created an Object named "local_data" which is associated with a shared object on the local hard-drive named user_data. Note that in the feature, this data can be read from other movies from the same domain that created the Shared Object.

Writing data
Lets store something in our fresh created Shared Object. Lets say, we want to store the user's name and his age in it. To do so, use this:

local_data.data.user_name = "John Smith";
local_data.data.user_age = 23;
local_data.flush()


Note that this code must be in the same level where you have created the Shared Object.

The flush() command is optional, it is used to write the information to disk immediately. If you don't use this command, Flash MX writes the shared object to a file when the SWF movie is closed or when the shared object is garbage-collected because it no longer has any references to it.

Add a comment