Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session variable

Status
Not open for further replies.

coldvision

Programmer
Feb 12, 2001
3
NL
Hello,
Can anybody show me how to set a session variable, and how to output its value again?
I know how to work with cookies, but i am looking for the same kind of variable that will expire after you shut down your browser, or leave a particular site! tnx
 
This example shows how I set session variables for a login page. I hope it helps or offers some insight.


<CFSET Session.LoggedIn = FALSE>

<--Then, I created a query that logs in users and appropriately resets the sessions variable(s) after it checks security level/validates passwords, etc-->

<cfquery name=&quot;QueryName&quot; datasource=&quot;#DSN#&quot; dbtype=&quot;ODBC&quot;>
SELECT *
FROM YourTable
WHERE (UserId = '#form.UserID#') AND (Password = '#form.password#')</cfquery>

<cfif QueryName.Record GREATER THAN 0>
<CFIF QueryName.Password IS &quot;#Form.Password#&quot;>
<CFSET Session.LoggedIn = TRUE>
<CFSet Session.UserID=&quot;#Form.UserID#&quot;>
<CFSet Session.Security=&quot;#QueryName.AuthCode#&quot;>
<CFSET Session.AddToken = &quot;cfid=#cfid#&cftoken=#cftoken#&quot;>


<CFELSE>
<CFSET Reason = &quot;The password you typed is invalid. Please Try again&quot;>

</CFIF>
<CFELSE>
<CFOUTPUT>
<CFSET Reason = Type your message here like unable to validate user named #Form.UserID#'>
</CFOUTPUT>

</CFIF>

<CFIF Session.LoggedIn>

<cflocation url=&quot;YourCFPage.cfm&quot; addtoken=&quot;Yes&quot;>


<cfelse>
<CFOUTPUT>

<Script>
alert(&quot;Sorry! Your login was unsuccessful&quot;);
self.location=&quot;WhateverPageYouWantHere.cfm&quot;;
</script>

</cfoutput>

</cfif>



 
Thanks a lot. The example you made is exactly what I wanted it to use for. But here's another question:
At one point you say:

<CFSet Session.UserID=&quot;#Form.UserID#&quot;>

Does it mean that I can use this variable on another page whitout losing its value? e.g.

<cfquery name=&quot;queryname&quot;
datasourse=&quot;mydatasource&quot;
dbtype=&quot;ODBC&quot;>
SELECT *
FROM MyTable
WHERE UserID=#session.UserID#
</cfquery>

I know you can call a cookie value whenever and whereever you want, but the problem is that you cannot let it expire when you quit your session.

I hope it's not a stupid question. I just started with CF.
It's cool!
Hope you can help me out!




 
Your question is not at all stupid - very valid.

I pass the variable from page-to-page as a hidden variable. This keeps it from expiring.

I'm sure if someone else has a better way that is more &quot;code efficient&quot;, they will share it with you!

Glad I was able to be of some assistance. Cold Fusion REALLY ROCKS!
 
There are many &quot;Scopes&quot; for variables in Cold fusion.

The &quot;Session&quot; scope has got to be the best (not knocking the Request Scope)

Session variables you set will be available &quot;magically&quot; to that user on any page he visits. (OK, not magic, through a combination of cookies/and or &quot;host headers&quot; for the cookie less browsers)

I have spent some time putting together what I hope is a &quot;clear&quot; tutorial on many of the scopes, if you are interested, here are two links you may like to see.

Session Variables:

Attributes, Request, and Caller scopes:

Application Scope:

Like Programmher says, CF rocks : )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top