The Source Code of ActionScript 3 Cookie class–Enables You to Read, Write, and Edit Shared Objects on the User’s Hard Disk

cookie-128x128 Have you ever wanted to store persistent information in your Flash movies, save login information, shopping cart data, user preferences, even complex objects like arrays? If you have purchased “Flash Extension”, simply import the cookie class and copy the source code like following:

  1. import com.communitymx.Cookie;
  2. var myCookie:Cookie = new Cookie(yoursite);
  3.  
  4. // user info
  5. myCookie.username = “nttlib”;
  6. myCookie.password = “abcdefg”;
  7. myCookie.save();
  8.  
  9. // retrieve cookie info
  10. trace(myCookie.username); // result: nttlib
  11. trace(myCookie.password); // result: abcdefg

But…Maybe you have noticed the word “purchased”–yes, it is not free! The following is a free, similar above cookie class, using it you can set cookies in flash to remember a user’s music preference now. :)

Download: cookie.as
  1. package com.nttlib.util { 
  2.     import flash.net.SharedObject;  
  3.     public class Cookie { 
  4.         private var _time:uint;
  5.         private var _name:String;
  6.         private var _so:SharedObject;
  7.         public function Cookie(name:String = "nttlib", timeOut:uint=3600) { 
  8.             _name = name;
  9.             _time = timeOut;
  10.             _so = SharedObject.getLocal(name, "/" );
  11.         }
  12.  
  13.         // clear when timeout;
  14.         public function clearTimeOut():void { 
  15.             var obj:* = _so.data.cookie;
  16.             if(obj == undefined){ 
  17.                 return;
  18.             } 
  19.             for(var key in obj){ 
  20.                 if(obj[key] == undefined || obj[key].time == undefined || isTimeOut(obj[key].time)){ 
  21.                     delete obj[key];
  22.                 } 
  23.             } 
  24.             _so.data.cookie = obj;
  25.             _so.flush();
  26.         } 
  27.         // check timeout
  28.         private function isTimeOut(time:uint):Boolean { 
  29.             var today:Date = new Date();        
  30.             return time + _time * 1000 < today.getTime();
  31.         } 
  32.         // get timeout;
  33.         public function getTimeOut():uint { 
  34.             return _time;
  35.         } 
  36.         // get cookie name;
  37.         public function getName():String { 
  38.             return _name;
  39.         } 
  40.         // clear all Cookie value;
  41.         public function clear():void { 
  42.             _so.clear();
  43.         } 
  44.         // add Cookie item( key-value )
  45.         public function put(key:String, value:*):void { 
  46.             var today:Date = new Date();
  47.             key = "key_"+key;
  48.             value.time = today.getTime();
  49.             if(_so.data.cookie == undefined){ 
  50.                 var obj:Object = {};
  51.                 obj[key] = value;
  52.                 _so.data.cookie = obj;
  53.             }else{ 
  54.                 _so.data.cookie[key] = value;
  55.             } 
  56.             _so.flush();
  57.         } 
  58.         // remove Cookie item by key;
  59.         public function remove(key:String):void { 
  60.             if (isExist(key)) { 
  61.                 delete _so.data.cookie["key_" + key];
  62.                 _so.flush();
  63.             } 
  64.         } 
  65.         // get Cookie item value by key;
  66.         public function get(key:String):Object{     
  67.             return isExist(key)?_so.data.cookie["key_"+key]:null;
  68.         } 
  69.         // check Cookie item exist;
  70.         public function isExist(key:String):Boolean{ 
  71.             key = "key_" + key
  72.             return _so.data.cookie != undefined && _so.data.cookie[key] != undefined;
  73.         } 
  74.     } 
  75. }

Cheers!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

14 Responses to “The Source Code of ActionScript 3 Cookie class–Enables You to Read, Write, and Edit Shared Objects on the User’s Hard Disk”

  1. [...] Cheat Sheet 3 hours agoMy mind is blah right now. Too sick for witty commentary. Well, whatever.The Source Code of ActionScript 3 Cookie class–Enables You to Read, Write, and Edit Shared Objects… 5 hours agoStore/save variables in cookies!msInterpolationMode Property (currentStyle, IMG, [...]

  2. Umer says:

    hey
    its not allowing to set time property?
    value.time = any thing you assign
    its give error #1056: can not create time property ?
    well i have removed that line, things started working smoothly:)
    do u have ne idea , if i dont use that line?
    also if i hav to use that line how can iget rid of that error?

  3. Gaggo says:

    Great Class, exactly what I needed!

    I have the same problem like Umer. Can you maybe go a bit more into detail about what the »time« thing has to do there and how I can use it right?

    Thanks!

  4. Gaggo says:

    Or can you maybe provide some examples of how to use it?

    That would be great!

  5. minidxer says:

    // new cookie object
    var cookie:Cookie = new Cookie(“myCookie”, 1000);
    //add key value
    cookie.put(“a”, {a:”a0001″});
    cookie.put(“b”, {b:2});
    //check exist
    trace(cookie.isExist(“a”));
    //remove key;
    cookie.remove(“a”);
    trace(cookie.isExist(“a”));
    //get value
    trace(cookie.get(“b”).b);
    //clear all value
    cookie.clear();

  6. [...] Have you ever wanted to store persistent information in your Flash movies, save login information, shopping cart data, user preferences, even complex objects like arrays? If you have purchased “Flash Extension”, simply import the cookie class and copy the source code like following:Have you ever wanted to store persistent information in your Flash movies, save login information, shopping cart data, user preferences, even complex objects like arrays? If you have purchased “Flash Extension”, simply import the cookie class and copy the link here. [...]

  7. [...] The Source Code of ActionScript 3 Cookie class–Enables You to Read, Write, and Edit Shared Obj… [...]

  8. [...] The Source Code of ActionScript 3 Cookie class–Enables You to Read, Write, and Edit Shared Obj… [...]

  9. Umer says:

    Gaggo
    well, i have removed that line from the code. hehe.. it working
    but ihavnt found solution
    but i have test it properly…
    you can skip that line.

  10. Scacek says:

    I am having errors when trying to use this class?? I tried using the example shown by minidxer and I receive the following error…

    1046: Type was not found or was not a compile-time constant: Cookie.

  11. Og2t says:

    hi Minidxer!

    Great class, thanks for sharing, exactly what I needed!
    I’ve found one bug in “isTimeOut” method: the “time” type has to be Number otherwise it will get truncated and returns wrong value (always true even for expired cookie items).

    I’ve also added a method hasExpired:

    public function hasExpired(key:String):Boolean
    {
    key = “key_” + key;

    var obj:* = _so.data.cookie;
    return isTimeOut(obj[key].time);
    }

    just to you could check if cookie had expired or not before clearing expired ones (might be useful sometimes).

    Thanks again!
    Tomek

  12. [...] bạn có thể tham khảo nguồn tại Ntt.cc Share and [...]

Leave a Reply