Using onSessionEnd in Application.cfc

Posted By : todd sharp Posted At : December 22, 2005 10:07 AM Posted In: ColdFusion

0

I've been reading on various forums lately about the onSessionEnd method available within Application.cfc. It seems there is some confusion about whether or not onSessionEnd actually works. I decided to throw together a quick demo:

Create an application and declare the onSessionStart method. In this case we'll set some session variables and create a directory on our server for use within the user's session (first thing that came to mind).

<cfcomponent displayname="Application">

<cfset this.name="test">
<cfset this.sessionmanagement="yes">
<cfset this.clientmanagement = "Yes">
<cfset sessiontimeout= createTimeSpan(0,0,20,0)>
<cfset applicationtimeout= createTimeSpan(1,0,0,0)>

<!--- To test this method, create a test page and add start=1 to the url --->
<cfif structkeyexists(url, "start")>
<cfset temp = onSessionStart()>
</cfif>

<cffunction name="onSessionStart">

    <cfscript>
    session.userinfo.thisPath=ExpandPath("*.*");
    session.userinfo.thisDirectory=GetDirectoryFromPath(session.userinfo.thisPath);
    session.userinfo.thisCreatedDirectory = #session.userinfo.thisDirectory#&"todd\";
    
</cfscript>
    
    <cfif not directoryexists(#session.userinfo.thisCreatedDirectory#)>
        <cfdirectory directory="#session.userinfo.thisCreatedDirectory#" name="dirQuery" action="create">
    </cfif>
</cffunction>


The key to the using the onSessionEnd method as stated in the CFML reference guide:

You must use the SessionScope parameter to access the Session scope. You cannot reference the Session scope directly; for example, use Arguments.SessionScope.myVariable, not Session.myVariable.

Keeping that in mind, my onSessionEnd method looks like this:

<!--- To test this method, create a test page and add end=1 to the url --->

<cfif structkeyexists(url, "end")>
<!--- call the onSessionStart --->
<cfset temp = onSessionStart()>
<!--- store the session in a var --->
<cfset sessionvars = session>
<!--- call the onSessionEnd, passing it the session structure--->
<cfset temp = onSessionEnd(sessionvars)>
</cfif>

<cffunction name="onSessionEnd">
<cfargument name="SessionScope" required="true">
    
    <cfif directoryexists(#Arguments.SessionScope.userinfo.thisCreatedDirectory#)>    
        <cfdirectory directory="#arguments.SessionScope.userinfo.thisCreatedDirectory#" name="dirQuery" action="delete">        
    </cfif>

</cffunction>
</cfcomponent>

Comments (0)