View comments | RSS feed

onSessionEnd

Description

Runs when a session ends.

Syntax

<cffunction name="onSessionEnd" returnType="void">
   <cfargument name="SessionScope" required=True/>
   <cfargument name="ApplicationScope" required=False/>
   ...
</cffunction>

See also

onSessionStart, Method summary, Managing sessions in Application.cfc in ColdFusion MX Developer's Guide

Parameters

ColdFusion MX passes the following parameters to the method:

Parameter Description

SessionScope

The Session scope

ApplicationScope

The Application scope

Returns

This method does not return a value; do not use the cfreturn tag.

Usage

Use this method for any clean-up activities when the session ends. A session ends when the session is inactive for the session time-out period. You can, for example, save session-related data, such as shopping cart contents or whether the user has not completed an order, in a database, or do any other required processing based on the user's status. You might also want to log the end of the session, or other session related information, to a file for diagnostic use.

If you call this method explicitly, ColdFusion does not end the session; it does execute the method code, but does not lock the Session.

You cannot use this method to display data on a user page, because it is not associated with a request.

You can access shared scope variables as follows:

Sessions do not end, and the onSessionEnd method is not called when an application ends. The onSessionEnd does not execute if there is no active application, however.

Example

The following method decrements an Application scope session count variable and logs the session length.

<cffunction name="onSessionEnd">
   <cfargument name = "SessionScope" required=true/>
   <cfargument name = "AppScope" required=true/>
   <cfset var sessionLength = TimeFormat(Now() - SessionScope.started,
      "H:mm:ss")>
   <cflock name="AppLock" timeout="5" type="Exclusive">
      <cfset Arguments.AppScope.sessions = Arguments.AppScope.sessions - 1>
   </cflock>
   <cflog file="#This.Name#" type="Information" 
      text="Session #Arguments.SessionScope.sessionid# ended. Length: #sessionLength# Active sessions: #Arguments.AppScope.sessions#>
</cffunction>

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


cameroncf said on May 23, 2005 at 3:52 PM :
Comment about the following line: "Sessions do not end, and the onSessionEnd method is not called when an application ends. The onSessionEnd does not execute if there is no active application, however."

This appears to describe a condition where onSessionEnd doesn't get called if the application ends before the session. If this is indeed what is being described, it's a condition that will only occur when the session timeout is longer than the application timeout. This might be a good place to say that in the docs.

IE: "To ensure that onSessionEnd runs when it should, always make your application timeout longer than your session timeout."
Brian Philippus said on Dec 2, 2005 at 7:19 AM :
FYI, The example contradicts the documentation regarding the required attribute of ApplicationScope argument.
Johan said on Sep 21, 2006 at 4:16 PM :
Is the example correct? It refers to arguments.AppScope where the documentation refers to arguments.ApplicationScope
halL said on Sep 22, 2006 at 10:08 AM :
In response to Johan: While the names are inconsistent, this is appropriate.
The arguments are positional and you can use any name for the arguments.
Oblio said on Jan 26, 2007 at 8:44 AM :
You may not create UDF's inside the function. For example, you may not:
<cfscript>
function myFunc() {
...
}
</cfscript>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000701.htm