Link to home
Start Free TrialLog in
Avatar of CFbubu
CFbubu

asked on

To find Coldfusion cookies possibility question. Thanks.

Hi folks,

I have yet another question and was wondering if this was possible.

Let's say I have 3 or more dynamically generated cookies named as such:

1) 'cookie.flower3345'
2) 'cookie.flower6578'
3) 'cookie.flower1234'

The dynamic part of each cookie above when it was being created was the 4 numbers generated behind after the word flower.

I know that to check if a page contains a certain cookie, I can use the statement below:

<cfif IsDefined('cookie.flower3345')>

There is a cookie for flower3345!

</cfif>

However, I was wondering if it were possible to check if a page containing any of the  3 or more 'flower' cookies?(There may be other cookies existing on the page but I am not interested in those) As these cookies are named dynamically, it would be hard to hardcode the names...

I was thinking that somehow, there might be a way to check for the existence of a cookie by searching for cookies with the word 'flower' in the front?

Now sure....

It would be awesome if I could get some light shed on this. Thanks very much guys!
Avatar of _agx_
_agx_
Flag of United States of America image

You could still use isDefined. Just plug in the the variable(s) containing the four numbers. Either of these would work:

<!--- simulate variable containing the numbers --->
<cfset theNumber = "3345">
<cfif IsDefined( "COOKIE.flower#theNumber#" )> found </cfif>

... or 

<cfset theNumber = "3345">
<cfif IsDefined("COOKIE.flower"& theNumber)> found</cfif>

... or 
<cfset someCookieName = "COOKIE.flower3345">
<cfif IsDefined( someCookieName )> found</cfif>

Open in new window


That said, all scopes in CF are structures. So you can do the same thing with structKeyExists. Generally it has the edge over isDefined because it doesn't have to searching through a bunch of scopes to find the variable name:

     <cfif structKeyExists( COOKIE, "flower"& theNumber)>
           found
     </cfif>

     ... or

     <cfset someCookieName = "flower3345">
     <cfif structKeyExists( COOKIE, someCookieName  )>
           found
     </cfif>
Avatar of CFbubu
CFbubu

ASKER

A great morning to you _agx_!

Hope your week has started well :)

Thanks for choosing to get back to my question!

I 'think' I get what you are telling me...let me try to implement your logic solution and see if that works....sometimes I am slow that way :P. I will get back to let you know how that goes!

Thanks again for making me feel I am not alone in my frustrations.
Yes, happy Monday to you too :)

No worries. Handling dynamic variables are one of those things that make more sense once you see it in action :)  The thing to remember is both IsDefined and structKeyExists expect the name of a variable.

     <cfif IsDefined( "nameOfSomeVariable" ) >
     <cfif structKeyExists( COOKIE, "nameOfSomeVariable" ) >

Meaning you're just passing in a simple string.  It doesn't really matter how you construct that string

     "COOKIE.flower#theNumber#"  .. or
     "COOKIE.flower"& theNumber
      ...

just as long as it ultimately evaluates to "COOKIE.flower3345"

       <cfif IsDefined( "COOKIE.flower3345" )>

... and "flower3345" when using structKeyExists

      <cfif structKeyExists(COOKIE,  "flower3345")>
Avatar of CFbubu

ASKER

Sorry I am taking abit long to get back. It's just that I am having alittle difficulty trying to apply what I am learning from you and need to ask a clarification question.  In your examples, the logic works when we 'know' what the random number is behind the flower cookie, but what if we don't? Thanks again for your trouble.

You see, this is what I am trying to do:

I have a page called 'setcookie.cfm' that sets a dynamic cookie, 'cookie.flower####'.

The above cookie is dynamic as the last 4 digits are kinda random.

I have another page, 'checkflowercookie.cfm' that is trying to 'detect' if there exists that dynamic flower cookie, there might be other different named cookies in existence besides the flower cookies.

1)Since I do not know what the value of the last 4 digits #### of any existing flower cookie, since it was set dynamically,  how to I check if a dynamic flower cookie has been set and does exists?  Is it possible to find the existence of cookies that start with a certain word string like 'flower'? even though the whole cookie name would be cookie.flower####?

2)If it was possible to accomplish the above, how than, will I be able to 'show' all the flower cookies that have been set?
SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFbubu

ASKER

Thanks so much for bearing with my questions.

I am getting excited with the possibility of getting to what I hope to accomplish with your generous answers :)

Let me go try 'massaging' my slow cells to get them to execute your solution :P

will be back with the exciting news!
Avatar of CFbubu

ASKER

I am not sure what I seem to be doing wrong. Sorry.

This is the code to set cookie on the setcookie.cfm page.

  <cfcookie name="flower#right(getnum.flower_ID,10)#" value="#right(getnum.flower_ID,10)#" expires="30"/>

The above will set a cookie that look like this:

cookie.flower6362FFD11D


This is the looping code on the checkcookies.cfm page

<cfloop collection="#COOKIE#" item="name">
     <!--- name starts with "flower" and ends with one or more numbers --->
     <cfif reFindNoCase("(^flower\d+$)", name)>
          <cfoutput>
              cookie name = #name# value = #cookie[name]#<br>
          </cfoutput>
     </cfif>
</cfloop>


I cannot seem to get the various flower cookies to show.... :(

If i use the statement below on the setcookie.cfm page, to check if the cookie was set, it shows 'it does exists'.

<cfif isDefined("cookie.flower#right(getnum.flower_ID,10)#")>
 
  it does exists

</cfif>

Maybe I am doing something wrong that I am not noticing, I am still trying to check and get the code to work.
Avatar of CFbubu

ASKER

ok..it seems that the solution you gave works only if there are no letters behind the word 'flower' during the cookie setting.

This means that if the name of the cookie is cookie.flower12345678, what's inside the cfloop will work. However, if the name of the cookie is cookie.flower1234fghr123(where the dynamic characters have both numbers and letters), what's inside the cfloop will not work...

<cfloop collection="#COOKIE#" item="name">
     <!--- name starts with "flower" and ends with one or more numbers --->
     <cfif reFindNoCase("(^flower\d+$)", name)>
          <cfoutput>
              cookie name = #name# value = #cookie[name]#<br>
          </cfoutput>
     </cfif>
</cfloop>

Would you be able to help with this ? thanks again for your kind trouble.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFbubu

ASKER

Yay!

Hope you had a good fill :)

I just did that and the cfloop shows now.....but how can I count how many times it has looped?
Avatar of CFbubu

ASKER

ok..got it. I figured a way to 'count' the number of loops.

But I realized that was apart from the actual question asked. :P

I just want to thank you for being so very kind with your time today :)
Avatar of CFbubu

ASKER

Awesome answers! Thank you.
Yeah, since it's a "collection" you have to implement your own counter variable.

You're welcome. Anytime :)
Avatar of CFbubu

ASKER

oh, by the way, how do you destroy the collection? :P
Normally you could do a structDelete() on the right scope, BUT ... you can't destroy the COOKIE collection.  It's indestructible ;-) Ok, maybe not indestructible but it's a system structure, so it'll always exist.  If you want to delete a cookie, use <cfcookie expire="..."> and it'll delete the cookie from the next request.
Avatar of CFbubu

ASKER

lol...it's like superman cookies....indestructible!  Thanks.

Well, it's been a great day having you around. Hope it went as well for you as it did for me(because of you).

Have a good evening and a good night:)
> lol...it's like superman cookies....indestructible!

Better than Popeye cookies (made with spinach ... ick!)