Accessing browser cookies from Flex
Asked Answered
M

3

13

I'm building a Flex widget for a private vBulletin site, and the Flex widget needs to access an XML file on the vBulletin server in order to display data.

For security reasons, the XML URL will need to have the value in the bbsessionhash cookie passed along in the URL request from Flex. The Flex widget will be embedded in the private area that the user has logged into, so the Flex request will be coming from the same website the cookie is from.

Is there any way to access the cookies directly within Flex? I would prefer not to use ExternalInterface to grab the cookie data from JavaScript, as it could get a little messy (the templates are developed by a completely different dev team).

Men answered 7/2, 2009 at 11:39 Comment(0)
T
13

I have never tried this, but this library might just do the trick.

Toritorie answered 7/2, 2009 at 12:2 Comment(1)
That is perfect, interestingly enough it actually uses ExternalInterface to write its own cookie-reading/writing JavaScript functions to the wrapper document. But hey, as long as the JS doesn't have to be embedded on the page itself it's good by me! Thanks!Men
A
2

As per the flash or flex cookies are concern developer can use shared object which is one kind of cookie used for flex application.

The sample code snippet is as followes

import flash.net.SharedObject;

// get/create the shared object with a unique name.
// If the shared object exists this grab it, if not
// then it will create a new one
var so: SharedObject = SharedObject.getLocal("UniqueName");

// the shared object has a propery named data, it's
// an object on which you can create, read, or modify
// properties (you can't set the data property itself!)
// you can check to see if it already has something set
// using hasOwnProperty, so we'll check if it has a var
// use it if it does, or set it to a default if it doesn't
if (so.data.hasOwnProperty("theProp"))
{
    trace("already has data! It reads: " + so.data.theProp);
}
else
{
    so.data.theProp = "default value";
    so.flush(); // flush saves the data
    trace("It didn't have a value, so we set it.");
}
Argument answered 9/5, 2011 at 16:11 Comment(1)
SharedObjects are better for your health indeed. Cookies will give you security errors and cross browser issues. Nice example here livedocs.adobe.com/flex/3/html/help.html?content=lsos_5.htmlAlmire
P
1

Accessing Flex SharedObject is NOT the same as accessing the Browser cookies, to access the browser cookies, you may use the ExternalInterface class, please check the following reference to see samples:

http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_4.html

A reference of how to use and control cookies using JavaScript can be found here:

http://www.quirksmode.org/js/cookies.html

I would use the following Flex code:

var myCookie:String = ExternalInterface.call("getCookie('cookieName')");

And in the HTML I would add the following Javascript:

function getCookie(c_name) {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++) {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name) return unescape(y);
  }
}

If you require more help you could also check the Flex documentation.

Peregrinate answered 4/11, 2011 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.