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

Logging out by session time limit

Session Management

Logging out by session time limit

by  webmigit  Posted    (Edited  )
I run a system somewhat like online banking.. And the staff loved remaining logged in.. However the users, who had balances with us, did not..

So I wrote what many developers have written and is really nothing special.. A session timeout script.

--- Before you read this, I suggest you read Glowball's faq232-1166 and my faq232-1926 both dealing with CFLOCK. ---

The first step is setting your variable.. I'm going to assume you use sessions, you could use cookies and do the same thing here.

Wherever you set your member variables add this

Code:
<cfset session.logtime=Now()>

or for you cookie people

Code:
<cfcookie expires="7" name="logtime" value="#Now()#">

You'll want to update this on each page so that its not 15 minutes from login, but 15 minutes from last activity. (Which of course means that if for some reason you wanted to limit total login time, you just wouldn't do this next step).

Code:
<cfset session.logtime=Now()>

or for you cookie people

Code:
<cfcookie expires="7" name="logtime" value="#Now()#">

Now finally.. in application.cfm or your header file or whatever you like (For those of you that aren't familiar, application.cfm at the beginning of each request meaning that if you type in a cfm page in your url, it will pull it up there but it will not execute it again before calling a custom tag or cfinclude since those are part of the same request).

Change loginidentifier to whatever your identifying variable is.. wheter it be session.logged or cookie.lastloggedin or whatever it is you use.

Change loggedinvalue to whatever value is specified for the loginidentifier variable meaning the user is logged in.

Also note the first parameter of datediff is "n".. "N" means minute because "m" means month.. Consult http://livedocs.macromedia.com/coldfusion/6/CFML_Reference/functions-pt158.htm for more information.

You can change 15 to any amount you like, this is speaking in terms of minutes.

Code:
<cfif loginidentifer eq loggedinvalue>
  <cfif datediff("n",session.logtime,now()) gt 15>
    <Cfset loginidentifier=notloggedinvalu>
    <cflocation user="/homepagefilename.cfm">
  </cfif>
</cfif>

Or for you cookie people

Code:
<cfif cookie.loginidentifer eq loggedinvalue>
  <cfif datediff("n",cookie.logtime,now()) gt 15>
    <cfcookie name="loginidentifier" value="notloggedinvalue" expires="7">
    ...cflocation does not allow cookies in the same document to set, I suggest you use javascript redirect or <Cfabort> or something...
  </cfif>
</cfif>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top