onSessionStart

Description

Runs when a session starts.

Syntax

<cffunction name="onSessionStart" returnType="void"> 
    ... 
</cffunction>

See also

onSessionEnd, Method summary, Managing sessions in Application.cfc in the Developing ColdFusion Applications

Returns

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

Usage

This method is useful for initializing Session scope data, such as a shopping cart, or setting session-specific Application scope variables, such as for tracking the number of active sessions. You need not lock the Session scope to set its variables using this method.

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

This method can access the requested page’s Variables scope only if the Application.cfc file includes an onRequest method that calls the page.

Example

The following onSessionStart example initializes some Session scope variables and increments an Application scope counter of active sessions.

<cffunction name="onSessionStart"> 
    <cfscript> 
        Session.started = now(); 
        Session.shoppingCart = StructNew(); 
        Session.shoppingCart.items =0; 
    </cfscript> 
        <cflock scope="Application" timeout="5" type="Exclusive"> 
            <cfset Application.sessions = Application.sessions + 1> 
    </cflock> 
</cffunction>