cfc中设置会话变量

cfc中设置会话变量

本文介绍了在Application.cfc中设置会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是ColdFusion 10的新手,特别是Application.cfc。这是非常混乱。
我创建了Application.cfc并且在此cfc中创建了以下内容:



我的问题是:

1.为什么我得到会话错误? (见下面的代码)


2.我应该在sessionEnd函数中放什么?

  <!--- Application.cfc ---> 
< CFCOMPONENT displayname =Applicationoutput =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 =OnApplicationStartaccess =public
returntype =booleanoutput =false>

< cfset application.Main_DSN =MyTestDB>

< / cffunction>



< cffunction name =onApplicationEndoutput =false>
< cfargument name =applicationScoperequired =true>

< / cffunction>


< cffunction name =OnSessionStartaccess =publicreturntype =voidoutput =false
hint =用户会话初始化时触发

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

< / cffunction>


<!---用户登录后,我有checklogin.cfm --->
< cfif mylogin NEQ true><!---登录失败,重定向到登录页--->
< cflocation url =login.cfm>
< cfelse>
< cfset session.loggedin =YES><!--- ???错误:见下文--->
< cfset session.username =#Trim(Form.username)#>
< cfset session.userrights =#Trim(Form.userrights)#>
< cfset session.usergroup =#Trim(Form.usergroup)#>
< / cfif>

< cffunction name =onSessionEnd>

<!---不知道我可以在这里放什么--->

< / cffunction>


解决方案

您的登录代码在任何事件处理程序之外您的Application.cfc文件,以便代码将在任何事件处理程序之前运行。



Application.cfc中的代码执行顺序是:




  • 任何事件处理程序之外的代码(无论文件在何处)

  • onApplicationStart
  • onSessionStart()
  • onRequestStart()




    • 所以你不能在其他事件处理程序之外引用会话范围(或者事件的应用程序范围)。只有 onSessionStart()已启动,才能开始使用会话变量。不是之前。


      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>
      
      解决方案

      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.

      这篇关于在Application.cfc中设置会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:31