Checking if Cookies are enabled

Hey guys

I am trying to figure out the best way to check if cookies are enabled on the user’s web browser. Any ideas? I looked and did not find anything…

Thank

Rick

  • Create a cookie with a string value
  • Try to read it. If the result is an empty string, cookies are not enabled.

Thank you Michel

A follow-up: I tried it in Xojo, but I always get a “cookie enabled” result even though a Website tells me they are disabled. Is it possible that Xojo always thinks that cookies are enabled or did I get the code wrong? Here is what I used:

[code]
// Checking if the browser accepts cookies

Dim ResultFromCookieTest as String = “”

Session.Cookies.Set(“CookieTest”, “This is a test”)

ResultFromCookieTest = Session.Cookies.Value(“CookieTest”)

If ResultFromCookieTest <> “” Then
MsgBox(“Cookies enabled”)
Else
MsgBox(“Cookies disabled”)
End If[/code]

Thanks again

Richard

As it stands, your code would work even if you received “Error” :

If ResultFromCookieTest <> "" Then MsgBox("Cookies enabled") Else MsgBox("Cookies disabled") End If

I would rather test specifically for the value I set the cookie with, and add a debuglog to make sure of the content :

System.debuglog ResultFromCookieTest If ResultFromCookieTest = "This is a test" Then MsgBox("Cookies enabled") Else MsgBox("Cookies disabled") End If

If you are on PC you need DebugView to see the message : http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

[quote=310227:@Richard Hille]Thank you Michel

A follow-up: I tried it in Xojo, but I always get a “cookie enabled” result even though a Website tells me they are disabled. Is it possible that Xojo always thinks that cookies are enabled or did I get the code wrong? Here is what I used:

[code]
// Checking if the browser accepts cookies

Dim ResultFromCookieTest as String = “”

Session.Cookies.Set(“CookieTest”, “This is a test”)

ResultFromCookieTest = Session.Cookies.Value(“CookieTest”)

If ResultFromCookieTest <> “” Then
MsgBox(“Cookies enabled”)
Else
MsgBox(“Cookies disabled”)
End If[/code]

Thanks again

Richard[/quote]
Cookies are stored in a Xojo class within each session, so what you’re doing will always show that the cookie is set.

Currently there is no built-in way to tell if cookies are enabled on the browser.

That said, someone could create a WebSDK control which would check this and fire an event…

http://www.w3schools.com/jsref/prop_nav_cookieenabled.asp

That should be easy enough to wrap up into a custom control with WebGen, I just don’t have the time right this very second.

CookieMonster.xojo_code (zip, 2kb)
Drop an instance on your webpage, check if cookies are enabled any time with the boolean CookieMonsterInstance.CookiesEnabled Sorry the icon is R2D2, FatCow didn’t have a Cookie Monster icon.

Made on lunch, so feel free to buy me lunch :wink:

Hey guys

Thanks for the replies.

@Tim Parnell : I tried your code and it seems like it is saying the cookies are always enabled. I tried looking into it to see if there might be an error but I have to admit, I did not find any issues… If you ever have a minute to look at it again, that would be great.

But, I find it surprising that there is no built-in way to check something so basic in Xojo… or am I the only one?

Thanks again

Rick

This is the first time I remember someone asking for this capability.

Cookies have gotten so taboo in recent years that it may be the that no one is using them any more either.

What’s your usage for this?

Hmm, are there websites without cookies? just had a look, in the last 6 hours about 400 cookies written on my system :wink:

The trick is to tell the user that the site needs cookies. Banks do that. But most commerce sites will fill your browser with all sorts of information, so they can recognize you, and they don’t tell, usually.

I am having no issues with it here, and it was also tested before I posted it.
Perhaps you’re not reloading your browser page after changing your cookie settings.

Hey guys

I agree with Michel and that was my intention all along: to let the user know that the site uses cookies and let him decide if he wants to enable them in order to avoid having to reselect the different settings like language and other stuff…

@ Tim Parnell: I tried it again this evening and it worked. I probably typed something wrong or maybe it was too late. But anyway, it works now. Thanks again! Now, I will try to set it up so the “Cookies Disabled” message disappears when the user changes his browser settings.

Thanks again to all

Rick

The browser may not update this right away. In Safari, the user must reload the tab to apply changes like that - it’s not a live updating value. Since I use Safari, and it’s free code, the module only checks once at page load for the cookies enabled value.