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 |
| » 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. |
| Download (0 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


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