Skip to main content
Inspiring
June 18, 2009
Answered

Hyphens and Cookies

  • June 18, 2009
  • 1 reply
  • 1279 views

I need to validate a cookie when a site redirects to my app.  The cookie name has a hyphen (-) in it.  When I say isDefined("cookie.abcd-name") I get an error saying cookie.abcd-name is not a syntactically valid variable name.

Am I not allowed to use these types of names?  I don't really get to pick the name of the cookie that's being passed to me.

Thank you!

    This topic has been closed for replies.
    Correct answer craigkaminsky

    You should be able to use StructKeyExists (the Cookie scope being a structure and all!) to get around this:

    <cfif StructKeyExists( cookie, "abcd-name" )>

         ... etc ...

    </cfif>

    ColdFusion will treat the hyphen as it is in IsDefined() as an arithmetic operator and try to subtract name from abcd whereas the above will evaluate properly.

    If you need to reference the cookie value, use the following notation:

    <cfset my_cookie = cookie["abcd-name"] />

    You'll run into the same issue you had with IsDefined() if you try cookie.abcd-name.

    Hope that helps!

    1 reply

    craigkaminskyCorrect answer
    Inspiring
    June 18, 2009

    You should be able to use StructKeyExists (the Cookie scope being a structure and all!) to get around this:

    <cfif StructKeyExists( cookie, "abcd-name" )>

         ... etc ...

    </cfif>

    ColdFusion will treat the hyphen as it is in IsDefined() as an arithmetic operator and try to subtract name from abcd whereas the above will evaluate properly.

    If you need to reference the cookie value, use the following notation:

    <cfset my_cookie = cookie["abcd-name"] />

    You'll run into the same issue you had with IsDefined() if you try cookie.abcd-name.

    Hope that helps!

    kodemonkiAuthor
    Inspiring
    June 19, 2009

    Excellent!  Thank you so much!