Skip to main content
Inspiring
February 24, 2019
Answered

calling a dynamically named cookie

  • February 24, 2019
  • 2 replies
  • 1346 views

Hi,

I'm trying to create a dynamic variable and then call it dynamically and I don't know if this is possible.  The easiest way to explain what I'm trying to do is to show what I want to do but obviously doesn't work.

<cfcookie name = "variable#rsSet.variable_id#" value="cookievalue">

and then later I want to call it by doing something like

<cfoutput>#cookie.#rsSet.variable_id##</cfoutput>

but obviously that doesn't work.  Is there a way to call the cookie without typing the exact name of the cookie?

Thanks,

Mike

    This topic has been closed for replies.
    Correct answer WolfShade

    I always use StructKeyExists(), never use isDefined() as I've heard of issues with it.

    V/r,

    ^ _ ^

    2 replies

    BKBK
    Community Expert
    Community Expert
    February 24, 2019

    wycks  wrote

    Hi,

    I'm trying to create a dynamic variable and then call it dynamically and I don't know if this is possible.  The easiest way to explain what I'm trying to do is to show what I want to do but obviously doesn't work.

    <cfcookie name = "variable#rsSet.variable_id#" value="cookievalue">

    and then later I want to call it by doing something like

    <cfoutput>#cookie.#rsSet.variable_id##</cfoutput>

    Do you mean <cfcookie name = "#rsSet.variable_id#" value="cookievalue">?

    If so then you could just do:

    <cfoutput>#cookie[rsSet.variable_id]#</cfoutput>

    wycksAuthor
    Inspiring
    February 24, 2019

    ok so i found out how to call the variable using

                    <cfoutput>evaluate("cookie.#rsSet.variable#")</cfoutput<

    but now my problem is that I need to check to see if it is defined so what I want to do (but obviously can't) is:

    <cfif isDefined("evaluate("cookie.#rsSet.variable#"))

    is there a way to check to see if the dynamically named cookie is defined?

    Thanks

    MIke

    BKBK
    Community Expert
    Community Expert
    February 24, 2019

    wycks  wrote

    <cfif isDefined("evaluate("cookie.#rsSet.variable#"))

    is there a way to check to see if the dynamically named cookie is defined?

    In this scenario, structkeyExists is perhaps better than isDefined. Something like

    <cfif structKeyExists(cookie, rsSet.variable_id)>

    </cfif>

    WolfShade
    WolfShadeCorrect answer
    Legend
    February 25, 2019

    I always use StructKeyExists(), never use isDefined() as I've heard of issues with it.

    V/r,

    ^ _ ^