AS2: SharedObject that expires like a cookie
People like to called the Flash SharedObject a “Flash Cookie”. But it’s not really a cookie and it never expires like a cookie. So here’s a “Flash Cookie” that expires. You can use it like this:
var c:Cookie = new Cookie("hello"); var d:Date = new Date(); // Make it last one hour d.setHours( d.getHours()+1 ); c.setValue("there", d );
This will create a cookie named “hello” with value “there” and it will last for one hour. For the never-ending cookie, don’t pass a date.
I haven’t tested this extensively, but it worked for what I needed it for.
You can get the class after the jump…
/* * author: Matt Shaw : matt at unformatt */ class Cookie { public static var SO_NAME:String = "cookie"; public static var SO_PATH:String = "/"; public static var EXPIRE:String = "_expire"; private var _so:SharedObject; private var _name:String; public function Cookie( name:String ) { _so = SharedObject.getLocal(SO_NAME,SO_PATH); _name=name; } public function setValue( v:Object, expires:Date ):Void { _so.data[_name] = v; _so.data[_name+EXPIRE] = expires; _so.flush(); } public function getValue():Object { var v:Object = _so.data[_name]; var expires:Date = _so.data[_name+EXPIRE]; if( expires && expires < new Date() ){ v = null; setValue(v); } return v; } }
3 Comments
Jump to comment form | comments rss [?] | trackback uri [?]