当前位置:  开发笔记 > 编程语言 > 正文

是否可以在Flash或Flex中评估存储cookie?

如何解决《是否可以在Flash或Flex中评估存储cookie?》经验,为你挑选了2个好方法。

在PHP中,有一个setcookie基于存储它的函数.在Flash中有可能吗?如果有可能那么如何?我想存储价值.



1> Simon Lehman..:

首先:PHP在服务器上运行,因此可以发送必要的HTTP标头来设置cookie,Flash在客户端的浏览器中运行,因此不能这样做.

但是,有一种方法可以通过使用flash.external.ExternalInterface和调用JavaScript函数来获取和设置cookie 来访问和存储来自flash/flex的cookie.

我已经开发了一个很容易做到这一点的课程:

package de.slashslash.util {

    import flash.external.ExternalInterface;

    /**
     * The Cookie class provides a simple way to create or access
     * cookies in the embedding HTML document of the application.
     * 
     */
    public class Cookie {

        /**
         * Flag if the class was properly initialized.
         */
        private static var _initialized:Boolean = false;

        /**
         * Name of the cookie.
         */
        private var _name:String;

        /**
         * Contents of the cookie.
         */
        private var _value:String;

        /**
         * Flag indicating if a cookie was just created. It is true
         * when the cookie did not exist before and false otherwise.
         */
        private var _isNew:Boolean;

        /**
         * Name of the external javascript function used for getting
         * cookie information.
         */
        private static const GET_COOKIE:String = "cookieGetCookie";

        /**
         * Name of the external javascript function used for setting
         * cookie information.
         */
        private static const SET_COOKIE:String = "cookieSetCookie";

        /**
         * Javascript code to define the GET_COOKIE function.
         */
        private static var FUNCTION_GET_COOKIE:String =
            "function () { " +
                "if (document." + GET_COOKIE + " == null) {" +
                    GET_COOKIE + " = function (name) { " + 
                        "if (document.cookie) {" + 
                            "cookies = document.cookie.split('; ');" + 
                            "for (i = 0; i < cookies.length; i++) {" + 
                                "param = cookies[i].split('=', 2);" + 
                                "if (decodeURIComponent(param[0]) == name) {" + 
                                    "value = decodeURIComponent(param[1]);" + 
                                    "return value;" + 
                                "}" + 
                            "}" + 
                        "}" + 
                        "return null;" + 
                    "};" +
                "}" +
            "}";

        /**
         * Javascript code to define the SET_COOKIE function.
         */
        private static var FUNCTION_SET_COOKIE:String =
            "function () { " +
                "if (document." + SET_COOKIE + " == null) {" +
                    SET_COOKIE + " = function (name, value) { " + 
                        "document.cookie = name + '=' + value;" + 
                    "};" +
                "}" +
            "}";

        /**
         * Initializes the class by injecting javascript code into
         * the embedding document. If the class was already initialized
         * before, this method does nothing.
         */
        private static function initialize():void {
            if (Cookie._initialized) {
                return;
            }

            if (!ExternalInterface.available) {
                throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");
            }

            // Add functions to DOM if they aren't already there
            ExternalInterface.call(FUNCTION_GET_COOKIE);
            ExternalInterface.call(FUNCTION_SET_COOKIE);

            Cookie._initialized = true;
        }

        /**
         * Creates a new Cookie object. If a cookie with the specified
         * name already exists, the existing value is used. Otherwise
         * a new cookie is created as soon as a value is assigned to it.
         * 
         * @param name The name of the cookie
         */
        public function Cookie(name:String) {
            Cookie.initialize();

            this._name = name;
            this._value = ExternalInterface.call(GET_COOKIE, name) as String;

            this._isNew = this._value == null;
        }

        /**
         * The name of the cookie.
         */
        public function get name():String {
            return this._name;
        }

        /**
         * The value of the cookie. If it is a new cookie, it is not
         * made persistent until a value is assigned to it.
         */
        public function get value():String {
            return this._value;
        }

        /**
         * @private
         */
        public function set value(value:String):void {
            this._value = value;

            ExternalInterface.call(SET_COOKIE, this._name, this._value);
        }

        /**
         * The isNew property indicates if the cookie
         * already exists or not.
         */
        public function get isNew():Boolean {
            return this._isNew;
        }
    }
}



2> Gdeglin..:

是.Flash应用程序可以在用户的​​计算机上存储多达100kb的数据(默认情况下).这存储在Flash Cookie中(与浏览器Cookie分开).用户可以通过右键单击Flash应用程序并进行设置来调整应用程序的存储量.

以下是AS3的SharedObject API:http: //livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/SharedObject.html

您可以通过Google搜索"Flash SharedObject"获取更多信息

确保准备好处理用户不允许您将数据放入SharedObjects的情况.这正成为关注其隐私的技术娴熟用户的流行趋势.

祝好运!

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有