First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing! | Flash Jobs
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
so.data.curData=Date;
so.data.flush;

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.

loadVariables("http://www.mysite.com/setCookie.php
?mycookie=name&myvalue=Bob",_root);

The code for the setCookie PHP is as follows

$mycookie=$_GET['mycookie'];
$myvalue=$_GET['myvalue'];
setcookie($mycookie,$myvalue,time() + 31536000);
?>

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

$myvalue=$_cookie['cookieName'];
//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: 6 Votes: 5
(10 being the highest)
» 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!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.