2 Replies Latest reply: Jun 19, 2009 5:35 AM by kodemonki RSS

    Hyphens and Cookies

    kodemonki Community Member

      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!

        • 1. Re: Hyphens and Cookies
          craigkaminsky Community Member

          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!

          • 2. Re: Hyphens and Cookies
            kodemonki Community Member

            Excellent!  Thank you so much!