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

Session Management

Status
Not open for further replies.

danielhai

Programmer
Oct 23, 2001
25
US
For some reason I can't get session management to work - i declared the session variable, i even put the cfapplication tag on every page, and my settings in the admin area are correct(i believe). Here's my code:

(login.cfm)

<CFAPPLICATION NAME=&quot;Security_Test&quot; CLIENTMANAGEMENT=&quot;YES&quot; SESSIONMANAGEMENT=&quot;YES&quot; SESSIONTIMEOUT=#CreateTimespan(0,0,30,0)#>
<cfif isDefined(&quot;Form.UserName&quot;) is true and isDefined(&quot;Form.Password&quot;) is true>

<cfquery datasource=&quot;chcf&quot; name=&quot;oConnLogin&quot;>
sp_login @UserName='#Replace(Form.UserName,&quot;'&quot;,&quot;''&quot;,&quot;ALL&quot;)#',@Password='#Replace(Form.Password,&quot;'&quot;,&quot;''&quot;,&quot;ALL&quot;)#'
</cfquery>

<cfif oConnLogin.recordcount is 0>
<cfinclude template=&quot;../inc/header.inc&quot;>
The login and password you entered was incorrect. Please re-enter the User Name and Password.
<cfelse>
<cflock timeout=2>
<cfSet Session.UserName = Form.UserName>
<cfSet Session.UserID = oConnLogin.ContactID>
</cflock>
<cflocation URL=&quot;/&quot; addtoken=&quot;yes&quot;>
</cfif>

</cfif>

(index.cfm)

<cfif isDefined(&quot;Session.UserID&quot;) is false>
<br>You are not currently logged in.<br><br>
<cfelse>
You are currently logged in as: <cfoutput>#Session.UserName#</cfoutput>
</cfif>
 
You cannot use CFLOCATION on a page that sets cookies, and SESSIONs use cookies.

A cookie is client side, whereas CFLOCATION is server side. A cookie is set when a page is fully loaded. If you use CFLOCATION it will redirect the page before the page fully loads. This is to your advantage if you don't want the entire page loaded. However, a cookie cannot be set using this. The alternative is to either use...

1. JavaScript:
a. location.href = &quot;mypage.cfm&quot;
b. location.replace(&quot;mypage.cfm&quot;) <-- I prefer this one

OR

2. Metatag
a. [COLOR=000080]<meta http-equiv=&quot;Refresh&quot; content=&quot;0 URL=&quot;mypage.cfm&quot;>[/color]

See:


A good example on creating a login script is:
- tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top