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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cold Fusion Session/Cleint Variable Question

Status
Not open for further replies.

monsemillis

Technical User
Jun 4, 2004
8
US
Hello, I am a fairly new user to cold fusion and have a quick question. here is what I want to do: I have created a login page which works fine....how can I pass other info in the database (i.e. first name, last name, company, id) to all other pages once they successfully login? Is this something for session management or client variables or something else? Thank you.
 
i usually set them to session variables upon login - although there are other alternatives but this generally works for me.
Code:
<cfquery datasource=#dsn# name="validateUser">
SELECT first,last,username,password,company_id FROM tbl_name WHERE ...
</cfquery>

<cfif validateUser.recordcount NEQ 1>
no users found
<cfelse>
<cfset session.first_name = #validateUser.first#>
<cfset session.last_name = #validateUser.last#>
<cfset session.company_id = #validateUser.company_id#>
</cfif>
 

You'll need to use CFAPPLICATION (link above) to name your application and set some basic options..

You'll also want to read the following FAQs

faq232-1166 Globall's Use CFLOCK around your session variables
faq232-1926 My Can I save myself from CFLOCKING Sessions?

And you might want to read

faq232-5138 My Logging out by session time limit

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Thanks for the info. Here is the application cf:

application.cfm
<CFSET Datasource = "dbcomp">

<cfapplication
Name="zscomp"
Sessionmanagement="yes"
sessionTimeout = #CreateTimeSpan(0, 2, 0, 0)#>

<cfinclude template="userlogin.cfm">

userlogin.cfm

<cfif not(IsDefined("Form.UserLogin") AND IsDefined("Form.UserPassword"))>
<cfinclude template="LoginPage.cfm">
<cfabort>

<cfelse>
<cfquery name="Client" datasource="#DataSource#">
SELECT UserID, Username, Password, FirstName, LastName, Company
FROM Clients
WHERE Username = '#FORM.UserLogin#' AND Password = '#FORM.UserPassword#'
</cfquery>

<cflock timeout = "90" scope = "SESSION" type = "readOnly">
<cfif Client.Recordcount NEQ 1>
No users found
<cfelse>
<cfset session.FirstName = #Client.FirstName#>
<cfset session.LastName = #Client.LastName#>
<cfset session.Company = #Client.Company#>
<cfset session.UserID = #Client.UserID#>
</cfif>
</cflock>

Question: (And I am sorry if this falls more into a SQL field) So can I now use a <cfquery> ...WHERE UserID = #session.UserID# and if UserID is related to a key in another table will I be able to pull information about posts the client has made to that table?

Thank you for your assistance...this is a great forum!
 
Sorry one more question: I am able to login to the main menu but once I click on another link it takes me back to the login page. Thanks again.
 
You keep going back to the login page because you have it set to do so in your application.cfm file. Once you log in, you need to set a session variable confirming the login. So you would need something like <cfset session.LoggedIn = "Yes"> when setting your other session variables (FirstName, LastName, etc...) Then in your application.cfm file you can have something like:
Code:
<cfif NOT IsDefined("Session.LoggedIn")>
  <cfset Session.LoggedIn = "No">
</cfif>
<cfif Session.LoggedIn EQ "No">
  <cfinclude template="userlogin.cfm">
</cfif>
This will probably need to be tweaked a little bit to better fit your application, but it should give you the basic idea.

By the way, don't name your Login Query "Client". Client is a reserved word in CF as it is what's used in the Client variable scope, like the word "Session". You could name it something like "Client_Login"...

And yes, "WHERE UserID = #session.UserID#" is fine to use. You can use a session variable in a query any time you want to pull something related to that session variable (provided the query/table/data has some type of matching value).



Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
Thanks for all the help guys...I sooo frustrated!! I have all my interior pages working but for some reason I am having a problem still. Here is what is happening: When I go to a page it displays zipscript.cfm...when I complete the form it sends me back to zipscript.cfm. Please point me in the right direction. Thanks....here is the code

application.cfm
<!---any variables set here can be used by all our pages--->
<cfset datasource = "software">

<!---Name our app, and enable Session variables--->
<cfapplication
name="zipscript"
sessionmanagement="yes"
sessiontimeout="#CreateTimeSpan(0, 2, 0, 0)#">

<!---If user is not logged in, force them to now--->
<cfif NOT IsDefined("SESSION.IsLoggedIn")>
<cfset SESSION.IsLoggedIn = "No">
</cfif>
<cfif SESSION.IsLoggedIn EQ "No">
<cfinclude template = "forceuserlogin.cfm">
</cfif>

forceuserlogin,cfm
<cfif not(IsDefined("Form.UserLogin") AND IsDefined("Form.UserPassword"))>
<cfinclude template="zipscript.cfm">
<cfabort>

<cfelse>
<cfquery name="GetUser" datasource="#DataSource#">
SELECT UserID, UserLogin, UserPassword, FirstName, LastName, Company, AuthLevel
FROM Users
WHERE UserLogin = '#FORM.UserLogin#' AND UserPassword = '#FORM.UserPassword#'
</cfquery>

<cflock timeout = "90" scope = "SESSION" type = "readOnly">
<cfif GetUser.Recordcount EQ 1>
<cfset session.IsLoggedIn EQ "Yes">
<cfset session.FirstName = #GetUser.FirstName#>
<cfset session.LastName = #GetUser.LastName#>
<cfset session.Company = #GetUser.Company#>
<cfset session.UserID = #GetUser.UserID#>
<cfset session.Role = #GetUser.AuthLevel#>
<cflocation url="zipscriptmenu.cfm">
<cfelse>
<script>
alert("You must login to access this area!");
</script>
<cfinclude template="ZipScript.cfm">
<cfabort>
</cfif>
</cflock>
</cfif>

zipscript.cfm FORM SECTION
<cfform action="forceuserlogin.cfm" name="UserLoginForm" method="Post">


<table border="0" align="center">
<tr bgcolor="#000000">
<th colspan="2"><font color="#FFFFFF">Please Log In</font></th>
</tr>
<tr>
<th><font color="#FFFFFF">UserName</font>:</th>
<td>
<!---Text field for "User Name"--->
<cfinput Type="text"
Name="UserLogin"
Size="20"
Value=""
Maxlength="100"
Required="yes"> </td>
</tr>
<tr>
<th><font color="#FFFFFF">Password:</font></th>
<td>
<!---Text field for "Password"--->
<cfinput Type="text"
Name="UserPassword"
Size="20"
Value=""
Maxlength="100"
Required="yes">
<!---Submit button that reads "enter"--->
<input type="submit" value="Enter"> </td>
</tr>
</table> <div align="center"></div>
</cfform></td>

zipscriptmenu.cfm QUERY and OUTPUT sections
<cfquery name="GetUser" datasource="#Datasource#">
SELECT *
FROM Users
</cfquery>

Thank you for any help!
 
It's never making it through the login process to set the session.IsLoggedIn value. You have to remember how CF works. It's going to load application.cfm before it loads anything else. So when you fill out your login form and it tries to process it, it loads application.cfm and sees you're not logged in and displays the login form again before you make it to the form processing section.

I'm sure there's a more efficient way to do this, but try this:
Code:
<cfif NOT #cgi.script_name# CONTAINS "login_processing_page_name.cfm">
<cfif NOT IsDefined("Session.LoggedIn")>
  <cfset Session.LoggedIn = "No">
</cfif>
<cfif Session.LoggedIn EQ "No">
  <cfinclude template="userlogin.cfm">
</cfif>
</cfif>

Like I said, that's pretty dirty, but maybe it will get you going until we can come up with a better solution...



Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top