6 Replies Latest reply on Aug 28, 2008 10:01 AM by siriiven

    Need help with application.cfc

    siriiven
      Here is what I am trying to do. I have a login page, and when a user logs in it verifies their credentials. If everything is good it sets a session variable called #session.username#. What I want to happen is at that point update a database field to set the user logged on status to 1. Everything to that point is fine. I am getting stuck at updating the databse when the users session ends. Before the session ends I would like to update the db field for the user and set the logged on status to 0.

      I haven't used application.cfc before. I have been messing around but nothing is working. Here is a copy of my application.cfc file. If anyone has any idea on how to run a query onsessionend and update a db I would appreciate it.

      <cfcomponent output="false">

      <cfset this.name = "test">
      <cfset this.applicationTimeout = createTimeSpan(0,2,0,0)>
      <cfset this.clientManagement = true>
      <cfset this.clientStorage = "registry">
      <cfset this.loginStorage = "session">
      <cfset this.sessionManagement = true>
      <cfset this.sessionTimeout = createTimeSpan(0,0,0,30)>
      <cfset this.setClientCookies = true>
      <cfset this.setDomainCookies = false>
      <cfset this.scriptProtect = false>



      <cffunction name="onApplicationStart" returnType="boolean" output="false">
      <cfset application.dsn = "dsn">

      </cffunction>



      <cffunction name="onSessionStart" returnType="void" output="false">
      <cfargument name="sessionScope" type="struct" required="true">
      <cfargument name="appScope" type="struct" required="false">



      </cffunction>
      <cffunction name="onSessionEnd" returnType="void" output="false">
      <cfargument name="sessionScope" type="struct" required="true">
      <cfargument name="appScope" type="struct" required="false">

      <!--- Update Logged On Status --->
      <cfquery datasource="dsn">

      UPDATE LoginNew
      SET loggedin = '0'
      WHERE username = '#session.loginname#'

      </cfquery>
      </cffunction>


      </cfcomponent>
        • 1. Re: Need help with application.cfc
          Dan Bracuk Level 5
          Your dilemma is that you are trying to use a session variable in the onSessionEnd function. By then, the variables are gone.

          I'd tell you how to solve this problem, except I don't know how to solve it myself.
          • 2. Re: Need help with application.cfc
            Level 7
            Dan Bracuk wrote:
            > Your dilemma is that you are trying to use a session variable in the
            > onSessionEnd function. By then, the variables are gone.
            >
            > I'd tell you how to solve this problem, except I don't know how to solve it
            > myself.
            >

            You have to use the arguments.SessionScope variable. A copy of the
            session scope is provide there on the onSessionEnd function.

            From the omnipresent ColdFusion documentation.

            <quote>
            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.
            </quote>
            • 3. Re: Need help with application.cfc
              siriiven Level 1
              Thanks for the replies. I will try that. Other than referencing the arguments.sessionscope.myvariable is there anything else with the file that looks not in order? I am a noob with application.cfc. I'm not sure if I have everything correct with it.
              • 4. Re: Need help with application.cfc
                siriiven Level 1
                Hmmm it is still not updating the database field. Here is my onsessionstart and onsessionend code:

                <cffunction name="onSessionStart" returnType="void" output="false">
                <cfargument name="sessionScope" type="struct" required="true">
                <cfargument name="appScope" type="struct" required="false">



                </cffunction>
                <cffunction name="onSessionEnd" returnType="void" output="false">
                <cfargument name="sessionScope" type="struct" required="true">
                <cfargument name="appScope" type="struct" required="false">

                <!--- Update Logged On Status --->
                <cfquery datasource="#application.dsn#">

                UPDATE LoginNew
                SET loggedin = '0'
                WHERE username = '#arguments.sessionScope.loginname#'

                </cfquery>
                </cffunction>


                </cfcomponent>

                Any ideas?
                • 5. Re: Need help with application.cfc
                  Level 7
                  siriiven wrote:
                  >
                  > Any ideas?
                  >

                  Take a peak at the documentation. It describes all the ins and outs...
                  you have to do the same thing with application variables as session
                  variables. The hint being that you are also defining an appScope
                  argument as well as the sessionScope argument, there is a reason for this.

                  <quote
                  src=" http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?cont ext=ColdFusion_Documentation&file=00000701.htm">
                  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 or, if using J2EE sessions, the user closes the browser. 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:

                  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.
                  You must use the ApplicationScope parameter to access the Application
                  scope. You cannot reference the Application scope directly; for example,
                  use Arguments.ApplicationScope.myVariable, not Application.myVariable.
                  Use a named lock when you reference variables in the Application scope,
                  as shown in the example.
                  You can access the Server scope directly; for example, Server.myVariable.
                  You cannot access the Request scope.
                  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.
                  </quote>
                  • 6. Re: Need help with application.cfc
                    siriiven Level 1
                    Ahhh I got it. My application was timing out before my session time out. I increased the application timroue and it's working now. Thank you all.