Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How can I detect if the browser accepts cookies?

This script must be placed in an empty page, without any content. Since the page will never be displayed to the browser. The "real" pages are yescookie.cfm and nocookie.cfm.

Please note we use server-side redirect (forward) instead of client-side redirect (<cflocation>) since search engine's spiders tend to penalize websites that perform client-side redirect.

As an added bonus, using getPageContext().forward() we keep the same url visible inside the browser's bar, allowing a better user experience and proper bookmarking.

<cfif structKeyExists(cookie, "tmtCookieTest")>
   <cfset getPageContext().forward("yescookie.cfm")>
<cfelseif NOT structKeyExists(url, "tmtCookieSend")>
   <!--- First time the user visit the page, set the cookie --->
   <cfcookie name="tmtCookieTest" value="Accepts cookies">
   <!--- The cookie was send, redirect and set the tmtCookieSend flag as an url variable --->
   <cfset getPageContext().forward("#cgi.script_name#?tmtCookieSend=true")>
<cfelseif structKeyExists(url, "tmtCookieSend")>
   <!--- We tried sending the cookie, no way, cookies are disabled, get out of here --->
   <cfset getPageContext().forward("nocookie.cfm")>
</cfif>


This question was written by Massimo Foti.
It was last updated on May 22, 2006 at 11:03:47 AM EDT.

CFML Referenced

<cfif>
GetPageContext()
<cflocation>

Categories

Application Management

Comments

Comment made by Adam Fairbanks (Tidy Technologies) on May 29, 2006 at 6:12 AM
Do you have an example somewhere of this working?

I don't see how the code can set a client-side cookie, and subsequently do a server-side redirect (server-side code is executed before client-side code). I would assume the code does a server-side redirect and never sets the client-side cookie. If so, this test will not work.


Comment made by Matt Barfoot on July 28, 2006 at 11:31 AM
I can only get this to work using CFLOCATION instead of getPageContext().forward.

I guess even with getPageContent().forward as the new page is served up it's sending http and therefore can send the cookie in the http header. Since no new http request has actually been sent back by the browser then it won't tell the server what cookies are set?

So is this recipe bad? Must bo a code mistake?


Comment made by Paul Alkema on February 9, 2009 at 12:04 PM
Thanks Massimo for the post!

For anyone looking for an easier simpler method to detect cookies, the urlSessionFormat() tag will append CFIDE information on the end of a form action filename. This can be used when checking for cookies as it has a built function to check cookies.

EXAMPLE <cfif urlSessionFormat('/') contains "CFID"> Cookies are not enabled <cfelse> Cookies are enabled </cfif>


Comment made by Alex Baban on March 5, 2009 at 2:34 PM
On any .cfm page you can use: <cfif IsBoolean(URLSessionFormat("True"))>cookies enabled<cfelse>cookies not enabled</cfif>

from: http://cflib.org/udf/isCookiesEnabled