Skip to main content
Inspiring
May 27, 2010
Question

intialise my session variables - in application.cfc

  • May 27, 2010
  • 1 reply
  • 704 views

Is it ok to initialise my session variables in the application.cfc ?

eg. whenever a user visits the website I need to display a couple of session variables in my INCLUDED page_header

session.shopping_items eg. 4

session.shopping_value eg. £95.00

so I need to initialise these variables at some point. I could do this in the page_header with isdefined() or I thought I could do it in the application.cfc

is it ok to do it in the application.cfc ?

eg.

<cffunction name="onSessionStart" returnType="void" output="false">

    <cfset session.unique_session_ref = CreateUUID()>

    <cfset session.shopping_items=0>

    <cfset session.shopping_total=0>

</cffunction>

Alternatively, instead of using session vars, I could just run a query on the "webCart" table and calculate the above values dynamically, but I thought that's an uneccesary performance hit ?

This topic has been closed for replies.

1 reply

ilssac
Inspiring
May 27, 2010

Dax Trajero wrote:

Is it ok to initialise my session variables in the application.cfc ?

Yup, that is exactly what the onSessionStart() function is for, as well as any other logic you would like to run once each time a new session is started for a user.

Inspiring
May 27, 2010

thanks Ian

Question - If I have the application and a session up and running, and decide to make a change like add a new session variables to the application.cfc, whats the best method to reset the application / session so it sees this new session variable ?

You see, I just added a new session variable, but the application cant see it ?

ilssac
Inspiring
May 27, 2010

My first choice:  Timeout the session and|or application scopes.  I.E. set the sessionTimeout and|or applicationTimeout value to zero.  Run the code to clear the variables.  Then set them back to the desired values.  Or just wait the set timeout period, twenty minutes by default for session state.

My second choice:  Rename that application.  All session and application scope data is tied to the applicaiton name.  Thus if you change the name of the applicaiton, i.e. this.applicaitonName value, you will get new scopes.  I usually do this only for a debuging or other testing purposes.

A third choice:  Clear the cookies in your browser.  Session state is tied to a client via the cfid and cftoken (or jsessionid) cookies.  If these cookies are not provided, ColdFusion will generate a new session scope for that user and provide new cookies.