Sanity Check Simple Cold Fusion Cookie / Redirect Code

Posted on 2008-11-04
Last Modified: 2010-04-21
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/">


Open in new window

Question by:unobserved
    LVL 16

    Expert Comment

    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.
    LVL 39

    Accepted Solution

    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">

    Open in new window

    LVL 1

    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
    I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now