Setting up Session variable in Application.cfc

故事扮演 提交于 2019-12-11 03:02:52

问题


Hi I'm very new with ColdFusion 10 especially with Application.cfc. It is very confusing. I created Application.cfc and inside this cfc I created the following:

My questions are:
1. Why do I get session error? (see my codes below)

2. What should I put in sessionEnd function?

  <!--- Application.cfc --->
   <CFCOMPONENT displayname="Application" output="true">

     <cfset THIS.Name = "MyTestApp" />
     <cfset THIS.ApplicationTimeout = CreateTimeSpan(0,0,60,0) />
     <cfset THIS.SessionManagement = true />
     <cfset THIS.SessionTimeout = CreateTimeSpan( 0, 0, 30, 0 ) />
     <cfset THIS.SetClientCookies = false />

    <cfset THIS.SetClientCookies = false />

    <cffunction name="OnApplicationStart" access="public" 
                                          returntype="boolean" output="false">

       <cfset application.Main_DSN = "MyTestDB">

    </cffunction>



    <cffunction name="onApplicationEnd" output="false"> 
       <cfargument name="applicationScope" required="true">

    </cffunction>


    <cffunction name="OnSessionStart" access="public" returntype="void" output="false" 
                                         hint="Fires when user session initializes">

       <cfset session.loggedin = "NO">
       <cfset session.username = "">
       <cfset session.userrights = "">
       <cfset session.usergroup = "">  

  </cffunction>


 <!--- After user login, I have checklogin.cfm --->
 <cfif mylogin NEQ true><!--- Login failed, go redirect to login page --->
    <cflocation url="login.cfm"> 
 <cfelse>
       <cfset session.loggedin = "YES"><!--- ??? error: see below --->
   <cfset session.username = "#Trim(Form.username)#">
   <cfset session.userrights = "#Trim(Form.userrights )#">
   <cfset session.usergroup = "#Trim(Form.usergroup)#">     
 </cfif>

 <cffunction name="onSessionEnd">

    <!--- Not sure what can I put in here????? --->    

 </cffunction>

回答1:


Your login code is outside any of the event handlers in your Application.cfc file, so that code will run first... before any of the event handlers!

The execution sequence of code in Application.cfc is:

  • code outside of any event handler (irrespective of where it is in the file)
  • onApplicationStart()
  • onSessionStart()
  • onRequestStart()
  • etc

So you cannot have code referencing the session scope (or application scope for that matter) outside the other event handlers. You can only start using session variables once onSessionStart() has fired. Not before.




回答2:


What is the error message that you are getting?

You can really put anything in the session end. Anything that you would need to run to clean up after a session.

Maybe you want to update a log or send an email, clear a cookie, or redirect to a certain page.

EDIT:

Is everything else in the Application.cfc working for you? It seems like the onSessionStart event is not firing.

EDIT 2:* Sorry for doing this via an answer but I need more rep to leave comments.

The session should start as soon as you connect to the application, you do not need to wait to be "logged in" as in verify credentials.

If the session variables do not exist then the event is not firing for some reason. Make sure you have the file name correctly and it is in the root folder of the application.

EDIT 3: Your login code is not in the onSessionStart function, not in any function. Unless I misunderstood your comment and you were saying that code section is in the "checklogin.cfm" file and not in the Application.cfc file.

EDIT 4: I need to jump on a client call but I can help some more after. We might need to take a look at part of the checklogin.cfm file.

EDIT 5: Any luck?

EDIT 6: Sorry for the delay, volume is ramping up this afternoon.

At this point I think I would either need to see the code that is doing the validation checking or ask if you are sure the validation is working correctly.

I assume you are connecting to a database, if you take the query you are using to verify the login credentials and run it in SQL server or whatnot inserting the values you would type into the form, do you get any results?

EDIT 7: Figure it out, OP?



来源:https://stackoverflow.com/questions/24210100/setting-up-session-variable-in-application-cfc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!