Solved

To find Coldfusion cookies possibility question. Thanks.

Posted on 2013-10-28
17
487 Views
Last Modified: 2013-10-29
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!
0
Comment
Question by:CFbubu
  • 10
  • 7
17 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 39606755
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>
0
 

Author Comment

by:CFbubu
ID: 39606827
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.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39606925
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")>
0
 

Author Comment

by:CFbubu
ID: 39607081
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?
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 500 total points
ID: 39607095
Oh .. my bad! I misread the question. I thought you knew the number ;-)  If you don't, you'll have to do what you mentioned earlier. Look through the cookies and find ones that start with "flower".

I can't test this so watch out for typos:

Edit: Fix typo

<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>
0
 

Author Comment

by:CFbubu
ID: 39607133
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!
0
 

Author Comment

by:CFbubu
ID: 39607176
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.
0
 

Author Comment

by:CFbubu
ID: 39607300
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39607333
Sorry for the delayed response. I was eating early for change :)

Yeah, if the value can have letters AND digits then you can either just check for "starts with 'flower') or do something more complicated like "also ends with x pattern of letters and numbers".  I'd start with a simple a simple left() check for now. Just replace the reFindNoCase with this:

        <cfif left(name, 6) eq "flower">
               found... output value like before
        </cfif>
0
 

Author Comment

by:CFbubu
ID: 39607349
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?
0
 

Author Comment

by:CFbubu
ID: 39607406
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 :)
0
 

Author Closing Comment

by:CFbubu
ID: 39607409
Awesome answers! Thank you.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39607421
Yeah, since it's a "collection" you have to implement your own counter variable.

You're welcome. Anytime :)
0
 

Author Comment

by:CFbubu
ID: 39607428
oh, by the way, how do you destroy the collection? :P
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39607477
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.
0
 

Author Comment

by:CFbubu
ID: 39607642
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:)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39609540
> lol...it's like superman cookies....indestructible!

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now