Solved

Setting and retrieving the value of a cfcookie

Posted on 2009-05-07
9
520 Views
Last Modified: 2013-12-24
I am trying to set a cookie and retrieve its value on another page.  For some reason it is not working. please examine my code and help me understand why.

Thanks in advance.
WHERE THE COOKIE IS SET........
<cfoutput>
               <cfset algorithm = "BLOWFISH">
	<cfset encoding = "HEX">
	<cfset cookieUsername = "#FORM.username#">
	<cfset cookiePassword = "#FORM.password#">
	<cfset cookieKey = generateSecretKey("#algorithm#")>
	<cfset cookiePasswordEncrypted= encrypt("#cookiePassword#", "#cookieKey#", "#algorithm#", "#encoding#")>
	<cfset cookiePasswordDecrypted=decrypt("#cookiePasswordEncrypted#", "#cookieKey#", "#algorithm#", "#encoding#")>
	<cfcookie name="username" value="#cookieUsername#" expires="#DateAdd("n",20,NOW())#" >
	<cfcookie name="passwordEncrypted" value="#cookiePasswordEncrypted#" expires="#DateAdd("n",20,NOW())#" >
	<cfcookie name="key" value="#cookieKey#" expires="#DateAdd("n",20,NOW())#">
</cfoutput>
 
 
WHERE THE COOKIES IS CALLED ON ANOTHER PAGE....
						<cfif isDefined("cookie.username")>
							Username::  <cfoutput>#cookie.username#</cfoutput>
						<cfelse>
							Cookie is NOT defined
						</cfif>

Open in new window

0
Comment
Question by:onaled777
  • 5
  • 3
9 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 400 total points
ID: 24328817
The cookie scopes are defined in Realms.
One relam is the folder level from web server root and all subfolders from that level.

If you want to be on the secure side to get allways all cookies from every folder level then set the cooke path to web server root folder, that is the single forward slash:
path="/"


<cfcookie name="username" path="/" value="#cookieUsername#" expires="#DateAdd("n",20,NOW())#" >

Open in new window

0
 

Author Comment

by:onaled777
ID: 24329381
Oddly enough adding the path="/" threw an error on my  page.

Nevertheless, the objective is to create a global cookie...one that is accessed from several domains.  

If you could throw some light on that too it would be appreciated.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 24329451
Several domains will ONLY work IF one common domain level exist.
Otherwise sharing cookies betwean domains is not possible.
The common domain means this:
You have three servers:
www1.company.com
www2.company.com
www3.company.com

Then the domain part of the cookie is: .company.com

If you have:
www1.dev.company.com
www2.app.company.com
www3.app.company.com

Then two levels would also work: .app.company.com

But what never will work is this:
www.company1.com
www.company2.com
www.company3.com

You have no common domain part and therefore no cookie sharing.
No cookie sharing also for web servers without domain name or IP addressing.



0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 63

Expert Comment

by:Zvonko
ID: 24329542
And you are right, for some strange reasons needs path= also the domain= attribute:
<cfcookie name="username" path="/" domain="" value="#cookieUsername#" expires="#DateAdd("n",20,NOW())#" >

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 24329553
Of course better is to put your real domain suffix there:
<cfcookie name="username" path="/" domain=".yourdomain.com" value="#cookieUsername#" expires="#DateAdd("n",20,NOW())#" >

Open in new window

0
 

Author Comment

by:onaled777
ID: 24330499
I have made the changes as you suggested yet I am still having trouble assessing the simple string::

#cfcookie.username#

from the index page of press.mydomain.com.

Any direction you can provide would be appreciated.
	<cfset algorithm = "BLOWFISH">
	<cfset encoding = "HEX">
	<cfset cookieUsername = "#FORM.username#">
	<cfset cookiePassword = "#FORM.password#">
	<cfset cookieKey = generateSecretKey("#algorithm#")>
	<cfset cookiePasswordEncrypted= encrypt("#cookiePassword#", "#cookieKey#", "#algorithm#", "#encoding#")>
	<cfset cookiePasswordDecrypted=decrypt("#cookiePasswordEncrypted#", "#cookieKey#", "#algorithm#", "#encoding#")>
	<cfcookie name="username" value="#cookieUsername#" expires="#DateAdd("n",20,NOW())#" path="/" domain=".mydomain.com">
	<cfcookie name="passwordEncrypted" value="#cookiePasswordEncrypted#" expires="#DateAdd("n",20,NOW())#" path="/" domain=".mydomain.com">
	<cfcookie name="key" value="#cookieKey#" expires="#DateAdd("n",20,NOW())#" path="/" domain=".mydomain.com">

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 24331088
It works for me when I put domain like this:
        <cfcookie name="username" value="#cookieUsername#" expires="#DateAdd("n",20,NOW())#" path="/" domain="">
        <cfcookie name="passwordEncrypted" value="#cookiePasswordEncrypted#" expires="#DateAdd("n",20,NOW())#" path="/" domain="">
        <cfcookie name="key" value="#cookieKey#" expires="#DateAdd("n",20,NOW())#" path="/" domain="">

Open in new window

0
 
LVL 27

Assisted Solution

by:azadisaryev
azadisaryev earned 100 total points
ID: 24332740
it works just fine for me without path or domain attributes on localhost...

make sure you DO NOT have a <cflocation> tag in same page that sets your cookies...


PS: iirc, path and domain attribs only useful for secure cookies, i.e. over SSL connection... and yes, they both must be specified.

Azadi
0
 

Author Comment

by:onaled777
ID: 24335803
thank you very much...I figured out that the code was simply located in the wrong branch of an if statement.  
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Domino Load Balancer For 2048 Key Requirement 7 47
PHP in Apache server 20 101
whm high memory usage in processes 7 103
Adding multiple JVM environments to RedHat 6 7 45
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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