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.

Categories

Application Management

Comments

comments powered by Disqus