[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

Sanity Check Simple Cold Fusion Cookie / Redirect Code

I have to provide a new splash page to a client to host on their server for an upcoming ad campaign.

The splash page runs all well and good, but there is a bit of code required on the home page to make sure that people visiting the site for the first time are served the splash page. The banner ads all point directly to the splash page, so on all links pointing from the splash page to the home page include a query string of ?refer=splash. On the home page we check to see if that query string exists and if so, server the standard home page instead of redirecting back to the splash page. Once the home page serves the splash page, we set a cookie so all future requests to the home page serve the home page.

Problem is, I'm not a Cold Fusion programmer and I'm trying to make this update as easy on my client's web guy as possible, so I just need the attached tiny snippet of code sanity checked before I hand it off. Any comments or changes would be much appreciated.
<!--
	if the visitor shows up with ?refer=splash in the query string it means they
	have come from the splash page. set the cookie to flag that they have seen
	the splash page and then show them the home page.
 
	if they have no query string, the user typed in the url or followed a link
	to the home page. see if the SERVEDSPLASH cookie is set. if it is *not* set,
	we set it and redirect them to the splash page. if it *is* set, we will do
	nothing except serve them the home page.
-->
 
<!-- incoming with query string -->
<cfif url.refer EQUAL "splash">
	<!-- set the cookie -->
	<cfcookie name="SERVEDSPLASH" value="1" expires="">
 
<!-- no query string and no cookie -->
<cfelseif cookie.SERVEDSPLASH NOT EQUAL 1>
	<!-- set the cookie -->
	<cfcookie name="SERVEDSPLASH" value="1" expires="">
 
	<!-- redirect to splash page -->
	<cflocation url="/splash/">
 
</cfif>

Open in new window

0
unobserved
Asked:
unobserved
1 Solution
 
duncancummingCommented:
It actually looks quite good at first glance.  The only thing I'd change would be turn your comments from HTML comments <-- to Coldfusion comments <--- (three hyphens not two).  Not entirely sure about the cookie stuff without referring to the documentation, but your logic seems fine.
0
 
gdemariaCommented:
There are some problems to address.
First, if the refer parameter is not on the URL or if the cookie is not set, your code will crash because the variable is not defined.  So you need to add isDefined() to both CFIF statements.

<cfif isDefined("url.refer") and url.refer is "splash">

<cfelseif isDefined("cookie.SERVEDSPLASH") and cookie.SERVEDSPLASH neq 1>

On the cflocation statement include the addToken=No parameter so you don't expose session variables (for security reasons)

<cflocation url="/splash/" addtoken="No">

On your cfcookie, do you want the cookie to last indefinately?  If so, enter NEVER in the expires parameter.  If you want it to be in effect only for this particular browsing, then remove the expires parameter all together.   Next time the user follows the link, he/she will see the splash screen again, but not for the rest of that visit.

<cfcookie name="SERVEDSPLASH" value="1">

Here's the big potential issue, you need to test.   You cannot do a CFLOCATION in the same request when you set a session variable or a cookie.   This is because the request needs to complete in order to have the setting take effect.   CFLOCATION prevents this from happening and thus the setting does not take place.  The good news is that you don't need it.   You don't need to set the cookie before going to the splash page, because when you come back from the spash page to the home page, the cookie is set then.   So the line can be removed.




<!-- incoming with query string -->
<cfif isDefined("url.refer") and url.refer is "splash">
 
   <!-- set the cookie -->
   <cfcookie name="SERVEDSPLASH" value="1" expires="NEVER">
 
<!-- no query string and no cookie -->
<cfelseif NOT isDefined("cookie.SERVEDSPLASH") or cookie.SERVEDSPLASH neq 1>
 
	<!-- redirect to splash page -->
	<cflocation url="/splash/" addtoken="No">
 
</cfif>

Open in new window

0
 
unobservedAuthor Commented:
Thanks for the help and the quick Cold Fusion lesson. Big thanks for pointing out the logic error, or at least, the fact that the logic went a little further than needed.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now