Flash Cookies

Ever want to be able to reference personalized information each time a user visits your Flash? Before Flash MX, you needed to use PHP cookies. But Macromedia/Adobe created Flash Cookies called SharedObjects to make this easier for you. I’m not kidding! It’s like two lines of code to save and retrieve locally-stored data each time a user browses the site.

Case and point, try entering your name and age to the Flash below. Then, click “Store” and either click the “Refresh” button, refresh your browser or hit F5.

 

This Flash movie cannot load. You are viewing this because:

1. You have no Flash Plug-In (Click here to download)

2. Your Flash Plug-in is older than the necessary version required to play this movie (Click here to reload the movie and click “Yes” when asked to upgrade your plug-in.)

 
That data is stored as a SharedObject in your Flash Plug-in directory on your local computer. Even if you close the browser and re-open it, that data remains for you. This is especially helpful if you’ve created a game or chat client or application that requires this basic data each time a user accesses the site. And while it’s mildy-invasive, it’s pretty similar to the same process most sites use for storing information locally through PHP.

By default, the player enables you to store 100kb of data for each domain. Should you try to store more than that, the Adobe Flash Player settings dialogue box will pop-up over your movie to ask the user to increase their information quota. They can also click a checkbox that denies Flash Cookies from saving anything to their computers. If this is the case, your Flash should present the standard fields for re-entering this data and storing it as variables inside the Flash, this way you can be sure your movie will run if it requires personalized data. In fact, I’d recommend not storing information to cookies that would disable your movie if not present even if you provide the aforementioned standard fields.

Here’s how it works. First, create a SharedObject on the first frame of your Flash:

var local_data:SharedObject = SharedObject.getLocal(“user_data”);

Then store whatever you want to it:

local_data.data.user_name = “Don”;

Then, optionally, you can force the Flash to store the data immediately, rather than waiting for the movie to be closed:

local_data.flush();

And, that’s it! Referencing the stored data is just as easy:

trace(local_data.data.user_name); //Outputs: Don

Feedback and questions always welcome.

Download Flash Cookie Example (RAR)
Includes:
 Flash FLA, Flash SWF, SWFObject
Looking for more Flash stuff?
Why not visit my company site:
www.era404.com

Leave a Reply

Your email address will not be published. Required fields are marked *