Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating Cold Fusion session Variables

Posted on 2003-10-23
22
Medium Priority
?
3,679 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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