How do I access cookies within Flash?

2019-01-22 09:29发布

I'm looking to grab cookie values for the same domain within a Flash movie. Is this possible?

Let's see I let a user set a variable foo and I store it using any web programming language. I can access it easily via that language, but I would like to access it via the Flash movie without passing it in via printing it within the HTML page.

7条回答
时光不老,我们不散
2楼-- · 2019-01-22 09:55

You can read and write cookies (Local Shared Object) from flash. Flash cookies are stored on your PC within a directory with the name of your domain. Those directories are located at:

[Root drive]:\Documents and Settings\[username]\Application Data\Macromedia\Flash Player\#SharedObjects\

This article from Adobe is a good start.

查看更多
欢心
3楼-- · 2019-01-22 09:56

If you just want to store and retrieve data, you probably want to use the SharedObject class. See Adobe's SharedObject reference for more details of that.

If you want to access the HTTP cookies, you'll need to use ExternalInterface to talk to javascript. The way we do that here is to have a helper class called HTTPCookies.

HTTPCookies.as:

import flash.external.ExternalInterface;

public class HTTPCookies
{
    public static function getCookie(key:String):*
    {
        return ExternalInterface.call("getCookie", key);
    }

    public static function setCookie(key:String, val:*):void
    {
        ExternalInterface.call("setCookie", key, val);
    }
}

You need to make sure you enable javascript using the 'allowScriptAccess' parameter in your flash object.

Then you need to create a pair of javascript functions, getCookie and setCookie, as follows (with thanks to quirksmode.org)

HTTPCookies.js:

function getCookie(key)
{
    var cookieValue = null;

    if (key)
    {
        var cookieSearch = key + "=";

        if (document.cookie)
        {
            var cookieArray = document.cookie.split(";");
            for (var i = 0; i < cookieArray.length; i++)
            {
                var cookieString = cookieArray[i];

                // skip past leading spaces
                while (cookieString.charAt(0) == ' ')
                {
                    cookieString = cookieString.substr(1);
                }

                // extract the actual value
                if (cookieString.indexOf(cookieSearch) == 0)
                {
                    cookieValue = cookieString.substr(cookieSearch.length);
                }
            }
        }
    }

    return cookieValue;
}

function setCookie(key, val)
{
    if (key)
    {
        var date = new Date();

        if (val != null)
        {
            // expires in one year
            date.setTime(date.getTime() + (365*24*60*60*1000));
            document.cookie = key + "=" + val + "; expires=" + date.toGMTString();
        }
        else
        {
            // expires yesterday
            date.setTime(date.getTime() - (24*60*60*1000));
            document.cookie = key + "=; expires=" + date.toGMTString();
        }
    }
}

Once you have HTTPCookies.as in your flash project, and HTTPCookies.js loaded from your web page, you should be able to call getCookie and setCookie from within your flash movie to get or set HTTP cookies.

This will only work for very simple values - strings or numbers - but for anything more complicated you really should be using SharedObject.

查看更多
▲ chillily
4楼-- · 2019-01-22 10:01

I'm 10 years too late. If you can embed the data you need in the page, it's 10 times easier to grab.

import flash.net.*

var _loader:URLLoader = new URLLoader();
var _req:URLRequest = new URLRequest('https://stackoverflow.com');
_loader.addEventListener(Event.COMPLETE, _onComplete);
_loader.load(_req);

function _onComplete(e:Event):void{
    var wantedData:RegExp = /<div class="cool-data">(.*?)</div>/ig;
    var result:Object = wantedData.exec(String(_loader.data));
    trace(result[0].split('<div class="cool-data">').join('')
                   .split('</div>').join(''));

}
查看更多
欢心
5楼-- · 2019-01-22 10:02

Some Googling shows that it can be done by using query strings:

For web applications, you can pass values to swf by url parameters, and (with action script inside swf) save them to the sandbox.

查看更多
Fickle 薄情
6楼-- · 2019-01-22 10:08

getCookie method in HTTPCookies.as should use "return" statement.

import flash.external.ExternalInterface;
public class HTTPCookies
{    
  public static function getCookie(key:String):*   
  {
        return ExternalInterface.call("getCookie", key);    
  }
  public static function setCookie(key:String, val:*):void 
  {
        ExternalInterface.call("setCookie", key, val);    
  }
}
查看更多
在下西门庆
7楼-- · 2019-01-22 10:12

cookies are available to javascript through document.cookie - try using flash's getURL to call a javascript function.

getURL('javascript:document.cookie = "varname=varvalue; expires=Thu, 2 Aug 2001 20:47:11 UTC; path="');

查看更多
登录 后发表回答