PDA

View Full Version : CookieJar Class...



MichaelxxOA
February 11th, 2006, 06:54 AM
As promised I am giving you guys my CookieJar class.. this is probably one of the simplest yet most useful utility classes I have written. It's extremely easy to use.. I've included inline with my post the class source and an example use as you would see it in an fla. I've also included a zip file of all the working source. Any feedback would be wonderful. Take Care guys.

_Michael


Code in the class:



import flash.external.ExternalInterface;
/*
* This class was created to manage Cookies from directly within flash
* Here we will eliminate the need to have an environment outside of flash
* maintain our cookies.
*
*@author: Michael Avila
*@version: 1.0
*/
class CookieJar
{

private static var write_cookie:String = "function writeCookie(args){var cookie_string = args[0] + \"=\" + escape(args[1]);alert(cookie_string);switch (args.length){case 3:cookie_string += \"expires=\" + args[2];break;case 4:cookie_string += \"path=\" + args[3];break;case 5:cookie_string += \"domain=\" + args[4];break;case 6:cookie_string += args[5];break;}document.cookie = cookie_string;}";

private static var get_cookie:String = "function getCookie(name){var allcookies = document.cookie;var pos = allcookies.indexOf(name + \"=\");if (pos != -1) {var start = allcookies.indexOf(\"=\", pos) + 1;var end = allcookies.indexOf(\";\", start);if (end == -1) end = allcookies.length;var value = allcookies.substring(start, end);value = unescape(value);return value;}}";

private static var remove_cookie:String = "function removeCookie(name){var cookie = name + \"=\";cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';document.cookie = cookie;}";

public static function setCookie(name:String, value:String, expires:Date, path:String, Domain:String, secure:Boolean):Void
{
ExternalInterface.call(write_cookie, arguments);
}

public static function readCookie(name:String):Object
{
return ExternalInterface.call(get_cookie, name);
}

public static function removeCookie(name:String):Void
{
ExternalInterface.call(remove_cookie, name);
}
}


Code in the .FLA:


import flash.external.ExternalInterface;


CookieJar.setCookie("flash_cookie", "Hello World");
alert("FlashCall: " + CookieJar.readCookie("flash_cookie"));

CookieJar.removeCookie("flash_cookie");
alert("FlashCall: " + CookieJar.readCookie("flash_cookie"));

function alert(value)
{
ExternalInterface.call("alert", value);
}

MichaelxxOA
February 11th, 2006, 07:15 AM
Note: In order for the examples ( and the class for that matter ) to work you have to run it on a web server. Take Care.

_Michael

This is something with cookies not with the class itself.

bombsledder
February 11th, 2006, 09:45 AM
in flash its easier just to use sharedObjects

hybrid101
February 11th, 2006, 10:03 AM
^yeah, but it's only on the local computer.
would it be possible to load the cookies off a database?

booler
February 11th, 2006, 10:04 AM
Nice class I don't suppose you want to write a tut about suing the class maybe some examples of when it could should be used.... great way to integrate cookies though :)

bombsledder
February 11th, 2006, 11:15 AM
^yeah, but it's only on the local computer.
would it be possible to load the cookies off a database?

and what do you think cookies are? they are data stored on a local computer

lol, you dont store cookies in a database, you store information in a database, cookies are made for local computer, just incase you didn't know because it sounds like you don't know

MichaelxxOA
February 11th, 2006, 04:38 PM
It's much easier for a server side language to grab a cookie than a shared object. I can write a tut, but it's probably going to be a few days.. I still have to finish the scrolling one, I'm swamped at work. I'm only able to post these because this is stuff I need for work so I've already made them. Take Care.

_Michael

booler
February 11th, 2006, 05:22 PM
Ha no worries take your time I'n not working on anything that could use this right now but I'm going to stuff it in my little black bag of tricks I keep :) though oddly enough I never use most of them :( hehehe still nice to have though

bombsledder
February 11th, 2006, 05:51 PM
your class is a good asset to have but in reality sharedobjects are so much easier to call in flash

MichaelxxOA
February 11th, 2006, 06:26 PM
Yes they are IN flash, but some of my projects necessitate the need for information to be passed to and from flash, cookies are a clean simple way of doing this. I am in no way saying to use this over sharedobjects, I'm just giving you guys another option if the need arises. Take Care.

_Michael

bombsledder
February 11th, 2006, 06:52 PM
i like the class its good but,

the only reason i would use this class is if i needed information outside of flash, if i only need information in flash sharedobjects works great

MichaelxxOA
February 11th, 2006, 07:11 PM
lol.. and that is good. This class wasnt created to replace sharedobjects, it is merely another tool. I dont understand the need to knock the class with a reasoning that it wasnt intended to do in the first place. Take Care.

booler: Thanks for the feedback.

_Michael

bombsledder
February 11th, 2006, 07:19 PM
:deranged: lol the whole time i thought you were trying to replace my friend shareobjects lol :(

MichaelxxOA
February 11th, 2006, 07:20 PM
ha, no way. They've never harmed me.. in fact they've done nothing but love on me. ;) Take Care.

_Michael

codemonkeypete
February 11th, 2006, 09:54 PM
Im going to sound rather lame, im sure, but i dont get it. I understand sharedObject(and have used that method a few times now), and i understand what cookies are and do - I guess what i dont understand is where I would actually need to use this class? ... If i wanted to store and retrieve data on the server side, wouldnt it be more logical to use a database? ... It seems more viable to use a (for example) php/mysql/xml setup for storiung data ...I guess this cookie class could be usfull for single line data storage?... sorry man, I just dont get it! (I want to though, enlighten me :D )

MichaelxxOA
February 11th, 2006, 10:46 PM
Cookies aren't a mechanism for storing large amounts of data. They are a way to allow a rather stateless environment to have some sort of memory that is not weighted on the server.

Let's say for an extremely simple example that you have to manage users in your current project. This means that a user has to be able to log into your site and retain that status until a time that you specify. Now in the case of shared objects we could log the user in and store a loggedIn value on the users machine, but let's say that Flash isn't not the only means of viewing this site. Let's say they click on a link that takes them to a non-flash page. In order for them to retain their logged in status you will have to have some way of storing that state in an accessible way. Sharedobjects while I believe are possible to read on the server side require more work. Cookies on the other hand are an easily accessible way of storing data.

This works in the reverse as well. Let's say that you have a site that is for the most part comprised of non-flash pages. This includes your logging in. When a user logs in and clicks on a Flash page there may be content that is accessible to logged in users, how does your flash movie know that the user viewing the swf is of the status logged in? This is where cookies can play a vital role in your project.

Like I said, this isn't a class written to save the world, it's just a tool. There may never be a time that requires you to use this class, and my simple example is probably only scratching the surface of what is possible. I'm simply listing it here so that in the event that someone needs this functionality it is available to them. Thanks for the feedback guys it is truly appreciated.

Codemonkeypete: I hope this helped answer your question a little bit. If you have any others or any feedback period, please feel free to comment.

Take Care.

_Michael

codemonkeypete
February 11th, 2006, 11:24 PM
no, good answer.. makes perfect sense now.. so, if i have a site with say, a flash chat, and a phpbb forum, cookies can actually keep the user data held for the user, so no double logins are needed.. ok, i see the merit in something like that...

MichaelxxOA
February 11th, 2006, 11:44 PM
Exactly. ;)

_Michael