Using Cookies in Flash

Okay, so we've all seen those sites that log your IP, tell you what system your PC is running and other odd details. There is a tutorial in our php section, that teaches you how to gather users information. Here however we are going to take a basic cookie written with JavaScript and will write the cookie info into Flash.

Here is the "raw" data that the script below generates for us:

<script language="JavaScript1.1" type="text/JavaScript1.1">
document.write(cookString);
// -->
< /script>

Now to do this cut a copy the script below and paste it into a html file. Then test it. You should get a list the same as the one above.

<html>
<head>
<title>knowledgeBASE JavaScript Cookies</title>
<SCRIPT LANGUAGE="JavaScript1.1">
<!--

    function loginfo(info) {
       var today = new Date()
       var expires = new Date()
       expires.setTime(today.getTime() + 1000*60*60*24*365)
       setCookie("DEMO", info, expires)
    }

    function setCookie(name, value, expire) {
       document.cookie = name + "=" + escape(value)
       + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
    }

var cookie_info
cookie_info = "&amp;height=" + screen.height
cookie_info += "&amp;width=" + screen.width
cookie_info += "&amp;colorDepth=" + screen.colorDepth
cookie_info += "&amp;appVersion=" + navigator.appVersion
cookie_info += "&amp;appName=" + navigator.appName
cookie_info += "&amp;cpu=" + navigator.cpuClass
cookie_info += "&amp;language=" + navigator.systemLanguage
cookie_info += "&amp;cookieEnabled =" + navigator.cookieEnabled

loginfo(cookie_info)

var cookString = "?";

if(document.cookie != "") {
    theCook = document.cookie.split(":")
       for (i = 0; i < theCook.length; i ++) {
       cookString += theCook[i]
    }
}

var cookString = unescape(cookString);
// -->
</SCRIPT>

</head>
<body bgcolor="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" text="#CCCCCC">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
<tr valign="middle" align="center">
<td height="18">

<SCRIPT LANGUAGE="JavaScript1.1">
    document.write(cookString);
// -->
</SCRIPT>


</td>
</tr>
</table>
</body>
</html>

Into Flash

Okay we've now got the browser writing out the details to the page. Now lets look at reading this information into flash. First of all remember that flash reads in variables in the format &name=value so in the above examples the screen height is written &height=768 . Now when I was looking at a few other tutorials which talk about reading these into flash, they have extra functions to add the & later. I found this to be troublesome and didn't always work. So I make myself add it at the start, this way I know the variables will read into flash correctly.

For this example we are going to use the fact that you can pass variables to a flash swf by adding them to the end of the name, like yourswf.swf&height=768 which will pass that variable into flash. In the above example we are using JavaScripts, document.write function to output the cookie onto the html page. To get JavaScript to write the tags to put the SWF onto the page & write the cookie variables on the end to pass them into flash.

You'll notice in the above example a section in italics, replace that section with this code, making sure you change "YOURSWF.swf" to fit your site, also the colour, and size variables.

<SCRIPT LANGUAGE="JavaScript1.1">
<!--
// Name the variables
flName = "YOURSWF.swf"
flColor = "#000000"
flHeight = "600"
flWidth = "800"

document.write('<OBJECT '
+ 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
+ ' codebase="http://active.macromedia.com/flash2/'
+ 'cabs/swflash.cab#version=4,0,0,0"'
+ ' ID="flash"'
+ ' WIDTH=' + flWidth
+ ' HEIGHT=' + flHeight + '>'
+ '<PARAM NAME=movie VALUE="' + flName + cookString + '">'
+ '<PARAM NAME=quality VALUE=high>'
+ '<PARAM NAME=menu VALUE=false>'
+ '<PARAM NAME=bgcolor VALUE=' + flColor + '>'
+ '<EMBED src="YOURSWF.swf' + cookString + '"'
+ ' name="flash"'
+ ' quality=high bgcolor=' + flColor
+ ' WIDTH=' + flWidth
+ ' HEIGHT=' + flHeight
+ ' TYPE="application/x-shockwave-flash"'
+ ' PLUGINSPAGE="http://www.macromedia.com/shockwave/'
+ 'download/index.cgi?P1_Prod_Version=ShockwaveFlash menu=false">'
+ '</EMBED></OBJECT>'
)
// -->
</SCRIPT>

All you need to do now is have a flash movie that has the following variables

image 1
Click to enlarge

height
width
colorDepth
appVersion
appName
cpu
language
cookieEnabled

image 2

As they say in the cooking shows "Here's one I prepared earlier"

Download file zip file includes, FLA, HTML & SWF files

Remember for flash to understand your variables the names must be preceeded with an & other than that it's rather simple. You can store lots of information in cookies, you can ask you users for information, write it too a cookie then get flash to read it back to you. Go for it, try it and see what you can do.


Close    To Top
  • Prev Article-Flash:
  • Next Article-Flash:
  • Now: Tutorial for Web and Software Design > Flash > Basic > Flash Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction