Solved

Creating Cold Fusion session Variables

Posted on 2003-10-23
22
3,671 Views
Last Modified: 2013-12-24
I need to set up a session variable containing a login ID, as cookies do not seem to be working.  How do I set up and access a session variable? Can I set it to disappear when the browser closes? The manuals I have are rather vague on the subject.  Any help is appreciated, thanks.  
0
Comment
Question by:TEFKASG
  • 7
  • 6
  • 3
  • +4
22 Comments
 
LVL 2

Expert Comment

by:jonnygo55
ID: 9611268
session variables by definition end when a person closes the browser...other than that they will last as long as set by default in CF administrator or by overriding with cfapplication:
<cfapplication sessionmanagement="Yes" sessiontimeout="#CreateTimeSpan(days,hours,minutes,seconds)#">

then set your session like
<cfset session.loginID = form.loginID>
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 9611396
As what jonnygo55 said, you must have that code <cfapplication> tag on the application.cfm.

Then once a session variable will be created you may now refer to that variable.

Here is an example in referring the session variable :
  <cfquery name="GetUser" datasource="dsn">
    SELECT * FROM Users
    WHERE loginID = '#session.loginID#'
  </cfquery>

But speaking of killing the session variables once the browser is close, you can have your code like this :

<cfif IsDefined("Cookie.CFID") AND IsDefined("Cookie.CFTOKEN")>
     <cfset cfid_local = Cookie.CFID>
     <cfset cftoken_local = Cookie.CFTOKEN>
     <cfcookie name="CFID" value="#cfid_local#">
     <cfcookie name="CFTOKEN" value="#cftoken_local#">
</cfif>

Be sure you have this on your application.cfm.

Goodluck !
eNTRANCE2002 :-)
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9611667
eNTRANCE2002 is exactly right.  Put that in your application.cfm and your cfid and cftoken will become session only cookies.

CJ
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 17

Expert Comment

by:anandkp
ID: 9612129
whats not working with cookies ?????

let me know - incase u need help !
0
 
LVL 3

Author Comment

by:TEFKASG
ID: 9616040
Well, I tried the session code and it did not work.  The <cfapplication> tag required a name - what's the purposes of that?

Here's the code I used:

<cfapplication sessionmanagement="Yes" name="Sipe">
<cfset session.loginID = 1>

Then elsewhere in the website I tried to access  #Session.loginID# and it does not exist.

Cookies started working for a little bit then stopped working again.  Ironically, one and only one cookie that I created for a different purpose works.  I am pulling my hair out.   I am also beginning to believe it is a server issue.  
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9616062
try this:

<cfapplication name="Sipe"
                     sessionmanagement="yes"
                     setclientcookies="yes"
                     sessiontimeout="#CreateTimeSpan(0,0,30,0)#"
                     SetDomainCookies="yes"
                     clientmanagement="no">
<CFLOCK TYPE="exclusive" SCOPE="SESSION">
  <CFSET SESSION.loginID = 1>
</CFLOCK>

the purpose of name is define this particular application and its variables (application/session/client)   A single CF server can run multiple applications and each could need its own Session/etc vars that need to be managed.  This attribute supports that.

CJ
0
 
LVL 3

Author Comment

by:TEFKASG
ID: 9616266
Ok, I tried that.  It didin't work either.  Part of the site uses frames - any chance that has something to do with it?
0
 
LVL 2

Expert Comment

by:jonnygo55
ID: 9616302
any chance that session varaibles not set in Cold Fusion Administrator?...
should be allowed by default but otherwise I am not sure what the problem is...frames shouldnt matter...
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9616434
Can you post your application.cfm and your code that checks if the var exists.

CJ
0
 
LVL 3

Author Comment

by:TEFKASG
ID: 9616487
Well, I just deleted a <cflocation> tag and the cookies are working again???

I don't actually have a application.cfm file per se.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9616534
oh, that makes sense.

cflocation tags prevent cookies from being set.  So your cfid and cftoken cookies, which identify your user's session were not being set.

Also, what do you mean you don't have an application.cfm file per se?

Do you automatically include your file in each file ro what?

CJ

0
 
LVL 3

Author Comment

by:TEFKASG
ID: 9616589
I have never needed a application.cfm file.  No global variables, in general.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9616656
so where do you put your cfapplication tag?

CJ
0
 
LVL 3

Author Comment

by:TEFKASG
ID: 9616759
I just picked a file in the path to where the session variables are needed.  How else should I have done it?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9617042
you should have an application.cfm file in the directory/app that is going to have session variables.

Then no matter what file the user first accesses, it is setup for session management.

CJ
0
 
LVL 2

Expert Comment

by:Crazee
ID: 9618240
To maintain session state, server has to know which client he is talking to ( or receives and posts data ). Think of it as a part of memory with an address bound to the certain client's ID. If identification won't be posted from every page by a client, the memory address will be lost, and you'll be no longer able to access the part of memory as you don't know how to refer to it.

No, there's why you have to use

<cfif IsDefined("Cookie.CFID") AND IsDefined("Cookie.CFTOKEN")>
     <cfset cfid_local = Cookie.CFID>
     <cfset cftoken_local = Cookie.CFTOKEN>
     <cfcookie name="CFID" value="#cfid_local#">
     <cfcookie name="CFTOKEN" value="#cftoken_local#">
</cfif>

posted by entrance2002

If you don't change cookies ( your ID ) on every page, whenever you go back to visit the page, the server will assume that the same session applies to you as the last time you visited. So if you close your browser and open it again and try the same site, your session variables will still be there ( untill timeout - usually 30 minutes ). So if you change cookies ( your ID ) on every page, every time the server is requested, it will bind new ID to your session variables. Then if you close the browser and don't send cookie ID, the next time you open the site your session will be lost cause you're sending new ID.

hope this is clear...
0
 
LVL 17

Accepted Solution

by:
anandkp earned 500 total points
ID: 9618695
the "CFLocation URL" wont quite work properly ... with cookies ...

remove ur cflocation tags & use this instead :
<CFHEADER NAME="Refresh" VALUE="0; URL=MainPage.cfm">
& incase u ahve parameters - sent them thru query string like
<CFHEADER NAME="Refresh" VALUE="0; URL=MainPage.cfm?param1=#param1value#">

let me know ...

K'Rgds
Anand
0
 
LVL 3

Author Comment

by:TEFKASG
ID: 9630662
anandkp>  that is exactly what I did after I removed the cflocation tag.

To all - This part of the project is on hold for the time being as it is working well enough as is - but I will be revisiting this problem in a week or two.  Thanks for your help and patience.
0
 
LVL 17

Expert Comment

by:anandkp
ID: 9631520
Didnt that work for cookies ???
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 9646712
Could it be that you just don't have cookies enabled?
Try to pass the CFToken through the URL, this will work if you can't work with cookies. However you need to pass it from every link like so
/dochome.htm?CFID=419&CFTOKEN=25745804

here's a bit of info on it
http://cfhub.com/advanced/managing_state/cookies.cfm
0
 
LVL 3

Author Comment

by:TEFKASG
ID: 10988630
I wasn't able to implement any of the solutions given, but  anandkp did give the solution to the problem I was originally having with cookies and the rest became a moot point after that.  Thanks all for your help.  :)
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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