Link to home
Start Free TrialLog in
Avatar of TEFKASG
TEFKASGFlag for United States of America

asked on

Creating Cold Fusion session Variables

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.  
Avatar of jonnygo55
jonnygo55

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>
Avatar of Renante Entera
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 :-)
eNTRANCE2002 is exactly right.  Put that in your application.cfm and your cfid and cftoken will become session only cookies.

CJ
whats not working with cookies ?????

let me know - incase u need help !
Avatar of TEFKASG

ASKER

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.  
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
Avatar of TEFKASG

ASKER

Ok, I tried that.  It didin't work either.  Part of the site uses frames - any chance that has something to do with it?
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...
Can you post your application.cfm and your code that checks if the var exists.

CJ
Avatar of TEFKASG

ASKER

Well, I just deleted a <cflocation> tag and the cookies are working again???

I don't actually have a application.cfm file per se.
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

Avatar of TEFKASG

ASKER

I have never needed a application.cfm file.  No global variables, in general.
so where do you put your cfapplication tag?

CJ
Avatar of TEFKASG

ASKER

I just picked a file in the path to where the session variables are needed.  How else should I have done it?
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
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...
ASKER CERTIFIED SOLUTION
Avatar of anandkp
anandkp
Flag of India 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 TEFKASG

ASKER

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.
Didnt that work for cookies ???
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
Avatar of TEFKASG

ASKER

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.  :)