Skip to main content
Known Participant
February 3, 2013
Question

test if cookies can save

  • February 3, 2013
  • 1 reply
  • 1398 views

Hi gang,

I'm sort of a rookie at this... delving into the saving of cookies. All's going well, I've got data collecting, sending off to a cookie, and I can retrieve it later.

Howerver, I was wondering what the best method to TEST if cookies can save. I was going to try pushing a tiny bit of data to my cookie right after

import flash.net.SharedObject;

var cookie:SharedObject = SharedObject.getLocal("myCookie");

var mySmallData:uint = 0

cookie.data.smallData = mySmallData

if (cookie.size > 0) {

     warningPopup.visible = false

} else {

     warningPopup.visible = true

}

It's working locally when I test if from Flash or run the swf from my desktop, but breaks when I run the swf in Chrome from my desktop. Is this even the right way to go about it? It feels clunky.

Be gentle, I'm learning.

dp

This topic has been closed for replies.

1 reply

sinious
Legend
February 3, 2013

You can use shared objects (a shiny word for cookies) but you can also just use ExternalInterface and let JavaScript manage the cookies.

Cookies are unpredictable and unreliable. Session management is a better strategy because it's so easy to not only disable cookies, but to clear cookies, limit cookies to sessions, wipe out after closing a browser, wipe after a certain number of requests, etc etc.

If it's vital to the function of your site I'd recommend going with sessions.

To answer you, just writing the shared object as you're doing (don't forget to flush() to write it) then testing should tell you. You issue might just be you didn't flush():

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html#flush()

Dan74Author
Known Participant
February 4, 2013

Thanks for pointing me in the right direction. I'm not making anything crucial, just a goofy little game, and I'm pretty tied into the shared object, so going to stick with it. I tried the flush approach (code below) but when I try it in Safari in Private mode, the popup doesn't appear.

     warningPopup.visible = true

     if (cookie.flush() == "flushed") {

          warningPopup.visible = false

     }

Is using Safari in private mode not a good litmus test this? It won't import any of the date FROM a shared object, so I figured it was not saving TO shared objects.

sinious
Legend
February 5, 2013

Private mode in any browser means cookies are restricted (to various extents). This is the unreliable nature you can't get around. If you detect you can't save a cookie you'll have to use a session approach (pass a unique key in every link, e.g. index.php?k=abcdefg12345) to a database entry of information or even serialize your cookie and encode it while passing it the same way.