Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I read URL Variables in Flash MX?

Status
Not open for further replies.

ColdFusionKing

Programmer
Dec 5, 2002
145
GB
Hi Guys, I want to know how to read cookies in Flash MX.

I'm embedding my flash movie on the html page, have added JavaScript to create a cookie which stores the username and password, I then want to send this information using URL variables to the flash movie and read the data using actionscript and authenticate the user.

Here is my code, I've knicked most of the logic to read and pass the cookie to flash in JavaScript from this site:

Here is the code:

<SCRIPT LANGUAGE=&quot;JavaScript1.1&quot;>
<!--
createCookie(&quot;Username&quot;,&quot;testUser&quot;);
createCookie(&quot;Password&quot;,&quot;testPassword&quot;);

function createCookie(name,value) {
var expireday = new Date();
expireday.setTime(expireday.valueOf() + 1000*3600*24*365);
document.cookie = name + &quot;=&quot; + value + &quot;; expires=&quot; + expireday.toGMTString() + &quot;;&quot;;
}

// Get Cookies script
cookString = &quot;?&quot;
// does the visitor have a cookie?
if(document.cookie != &quot;&quot;) {
theCook = document.cookie.split(&quot;; &quot;)
// add each part of the cookie to a string variable
for (i = 0; i < theCook.length; i ++) {
cookString += theCook + &quot;&&quot;
}
}

// Name the variables
flName = &quot;myFlashmovie.swf&quot;
flColor = &quot;#99cc99&quot;
flHeight = &quot;300&quot;
flWidth = &quot;400&quot;
// This portion of the script is a modified version of Colin Moock's
// flash cookie importer, query string version script.
// Slight modifications have been made to
// work with the get cookies script.
document.write('<OBJECT '
+ 'classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot;'
+ 'codebase=&quot; + 'cabs/flash/swflash.cab#version=6,0,0,0&quot;'
+ ' ID=&quot;graph&quot;'
+ ' WIDTH=680'
+ ' HEIGHT=600>'
+ '<PARAM NAME=movie VALUE=&quot;' + flName + cookString + '&quot;>'
+ '<PARAM NAME=quality VALUE=high>'
+ '<PARAM NAME=bgcolor VALUE=#FFFFFF>'
+ '<EMBED src=&quot;myFlashmovie.swf' + cookString + '&quot;'
+ ' name=&quot;flash&quot;'
+ ' quality=high bgcolor=#FFFFFF'
+ ' WIDTH=680'
+ ' HEIGHT=600'
+ ' TYPE=&quot;application/x-shockwave-flash&quot;'
+ ' PLUGINSPAGE=&quot; + '</EMBED></OBJECT>'
)
// -->
</SCRIPT>

If someone's got a better solution to do this, then please let me know.

Could somebody show me how to read the URL variables in Flash MX?
 
The imported variables will be accessible as _root.name and _root.expires - if you need to unURLencode them use:

name=unescape(_root.name);
 
Thank you wangbar! I've used it and it works fine. I want you to look at this code and tell me if it is safe. I've read somewhere that wrapping the object and embed tag around the document.write javascript function is not safe? Is that right? Is there a better way to do this.

tmpVar = &quot;&quot;;
// does the visitor have a cookie?
if(document.cookie != &quot;&quot;) {
theCook = document.cookie.split(&quot;; &quot;)
// add each part of the cookie to a string variable
for (i = 0; i < theCook.length; i ++) {
tmpVar += theCook + &quot;&&quot;;
}
}

document.write('<OBJECT classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot; codebase=&quot; WIDTH=&quot;680&quot; HEIGHT=&quot;600&quot; id=&quot;myflashMovie&quot; ALIGN=&quot;&quot;>');
document.write(' <PARAM NAME=movie VALUE=&quot;myflashMovie.swf&quot;> <PARAM NAME=FlashVars VALUE=&quot;' + tmpVar + '&quot;> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF>');
document.write(' <EMBED src=&quot;myflashMovie.swf&quot; quality=high bgcolor=#FFFFFF WIDTH=&quot;680&quot; HEIGHT=&quot;600&quot; FlashVars=&quot;' + tmpVar + '&quot; NAME=&quot;myflashMovie&quot; ALIGN=&quot;&quot; TYPE=&quot;application/x-shockwave-flash&quot; PLUGINSPAGE=&quot; </EMBED>');
document.write('</OBJECT>');


The reason why I'm wrapping this code around javacript is because I want to read the cookie and pass the information to the flashVars param attribute.

<PARAM NAME=FlashVars VALUE=&quot;' + tmpVar + '&quot;>

I'm aware of another alternative way of doing this, see the code below:

<OBJECT classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot;
codebase=&quot; WIDTH=&quot;680&quot;
HEIGHT=&quot;600&quot;
id=&quot;myflashMovie&quot;
ALIGN=&quot;&quot; />

<PARAM NAME=movie VALUE=&quot;myflashMovie.swf&quot; />
<PARAM NAME=quality VALUE=high />
<PARAM NAME=bgcolor VALUE=#FFFFFF />

<PARAM NAME=FlashVars
VALUE=&quot;&quot; />


<EMBED src=&quot;myflashMovie.swf&quot;
quality=&quot;high &quot;
bgcolor=&quot;#FFFFFF&quot;
WIDTH=&quot;680&quot;
HEIGHT=&quot;600&quot;
NAME=&quot;myflashMovie&quot;
ALIGN=&quot;&quot;
TYPE=&quot;application/x-shockwave-flash&quot;
PLUGINSPAGE=&quot; />

</OBJECT>

The problem in the code above is I don't know how to pass the javascript variable &quot;tmpVar&quot; to the FlashVars params attribute

<PARAM NAME=FlashVars VALUE=&quot;&quot; /> ???

I'll really appreciate your help.
 
I use the first method all the time and have had no problems with it. In the end all you're doing is writing some HTML into the browser, writing it dynamically doesn't strike me as any less safe than picking up a static file off the server (unless you have a really picky client). Let's face it if stablility and security are really you're over-riding concerns you wouldn't be using cookies in the first place.

By the way, if you're using MX, have a look at local shared objects - it's the Flash cookie equivalent but much more flexible and can be picked up directly from your movie withoput any javascript.
 
I think you're right, I'll go with the first method. Thanks mate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top