How do I know when a user's session ends?
You can run code when a user session end's by using the onSessionEnd method of the Application.cfc file. The following example will log to a file:
<cffunction name="onSessionEnd" returnType="void" output="false"> <cfargument name="sessionScope" type="struct" required="true"> <cfargument name="appScope" type="struct" required="false"> <cflog file="#arguments.appScope.applicationName#" text="Session ended."> </cffunction>
Note that within the onSessionEnd method you cannot address the session or application scopes directly. Instead you reference them via the arguments automatically passed to the method. You can't output anything from this method since, obviously, no user is around to see the output.
Lastly - if you use J2EE session variables, you will notice that your session ends when your close the browser. Actually, your session is not ending. Instead, when you relaunch your browser, a new session is created. Therefore you cannot close your browser as a means to test this code. Instead, you must wait the complete amount of time it takes for sessions to expire in your application. (If not specified, it will default to a value set in the ColdFusion administrator - usually 20 minutes.)
This question was written by Raymond Camden
It was last updated on January 16, 2006.