3

Possible Duplicate:
Calling a variable with a variable in its name - coldfusion?

Sorry, I'm a bit of a newbie at Coldfusion. Trying to update some legacy code at work...

I named a cookie in coldfusion using a variable.

<cfcookie name="#FORM.cookie_name#" Expires="#FORM.cookie_expires#">

How do I check to see if the cookie hasn't expired? This seems to crash the page...

<cfif isDefined("cookie.#FORM.cookie_name#")>

Many thanks.

Community
  • 1
  • 1
r00tAcc3ss
  • 1,281
  • 3
  • 10
  • 9
  • 3
  • Please send us an edited version of the error. – J.T. Sep 17 '12 at 14:51
  • That worked! Thanks! If you add it as an answer I'll mark it as correct. – r00tAcc3ss Sep 17 '12 at 14:53
  • 1
    _"This seems to crash the page"_ - it "seems" to crash the page, or it crashes the page? What does "crash the page" mean - provide the full error (with sensitive information sanitized). Also state what the value of form.cookie_name is - is it alphanumeric or does it contain other characters. What happens when you do ``, etc – Peter Boughton Sep 17 '12 at 14:53
  • Dynamic IsDefineds are ok. is valid. Define FORM.cookie_name and it'll work. Is your error "cookie_name is not defined in FORM"? – Adrian Lynch Sep 18 '12 at 08:26

2 Answers2

8

To check if it exists, use StructKeyExists:

StructKeyExists(Cookie,Form.cookie_name)

To use the variable, use bracket notation:

Cookie[Form.cookie_name]


Don't forget both Form variables and Cookie variables are submitted by the user/client - and thus must be treated appropriately (i.e. never trust that they contain what you think they do, because it's not guaranteed).



Note, if you did want to use the isDefined form to do this (though there's benefit to it), it would need to be written like so:
isDefined("Cookie['#Form.cookie_name#']")

Which would then be evaluated as Cookie['606ac80d'] - without those single quotes it would be an invalid variable.

As noted by Leigh, the above works in Railo and OpenBD, but not in Adobe ColdFusion, where you would need to write:

isDefined("Cookie.#Form.cookie_name#")

This syntax will work for simple alphanumeric variable names, but specifically may cause issues if form.cookie_name contained a value with a . in.

Seybsen
  • 14,989
  • 4
  • 40
  • 73
Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • 1
    Re: *isDefined("Cookie['#Form.cookie_name#']")* that syntax is valid for Railo and BlueDragon/OpenBlueDragon. But for Adobe CF, you need to drop the square brackets ie `IsDefined("Cookie.#form.cookie_name#")` – Leigh Sep 17 '12 at 15:14
  • Bah, typical. I'm tempted to start putting a standard disclaimer _"any syntax that seems reasonable will work in Railo and OpenBD but may or not work in ACF depending on the whims of the Adobe engineers"_. :C – Peter Boughton Sep 17 '12 at 15:28
  • Haha, I never saw the logic behind that particular decision either. Yet another reason I moved to using `structKeyExists`. – Leigh Sep 17 '12 at 15:42
-1

Hope this helps :)

<cfcookie name="#form.cookie_name#" Expires="#form.cookie_expires#">
<cfif structKeyExists(cookie, form.cookie_name)>
Cookie
</cfif>

Similar question to this post

Community
  • 1
  • 1
JamesRLamar
  • 908
  • 1
  • 11
  • 22