First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: William Clarkson | Website: http://www.nerds.clarksontechnology.com

Setting a cookie with Flash

Most web programmers know the value of using a cookie to store data between a users visits. Cookies keep passwords, shopping carts contents and preferences of many kinds stored on the user's computer until they need it. Flash provides us with the shared object that can either be set to be accessed only by the file that made it, or for any other swf file. For Example:

//make shared object and read values
var so:SharedObject = SharedObject.getLocal("foo", "/");
uname = so.data.email2;
pass2 = so.data.pass2;
email = so.data.email;
//write to sharedObject

This works great if you want flash to read the variables later, but what if you need to access information in php but that has been written by flash? Or if you need to read in flash a cookie set by php?

Flash contains a command called loadVars() which as one may guess was made for loading a variable into flash. However when flash tries to load a variable from a php page all the script on that page is executed, thus giving all the power of PHP to Flash.

So to set a cookie from flash or to execute any php script from flash we only need one line of action script.


The code for the setCookie PHP is as follows

setcookie($mycookie,$myvalue,time() + 31536000);

to read the cookie in Flash or PHP later use this script:

//write out for flash
echo "&myvalue=$myvalue";

you can call this or any other php script by using the loadVariables method above. This will allow you to hook flash to Databases, Rss and many other web services

» Level Intermediate

Added: 2006-06-09
Rating: 5.17 Votes: 6
» Author
William Clarkson is a flash programer who has worked throughout the US and Asia. He now runs several websites and a consuliting company in Wisconsin
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.