【问题标题】:User data is getting mixed up some of the time用户数据有时会混淆
【发布时间】:2014-01-01 18:34:33
【问题描述】:

我正在构建一个网站,我在其中遵循 MVC 来管理我的代码,而不使用任何框架。我已将所有查询放入 cfcs 并在我的 Application.cfm 中对其进行初始化,并将它们存储在如下应用程序变量中:

<cfset aplication.customerProfileObject=   
                createObject("component","cfc.customerprofile").init()>

为了执行任何查询操作,我创建了一个函数,然后像这样在任何地方调用它:

<cfset selectedCustomerOb =   
      application.customerProfileObject.getContactCustomerProfileDetail(session.userid)>

我不知道是什么导致了问题,但有时用户会访问其他用户的数据。这怎么可能?它是在评估另一个用户的会话数据还是我初始化了 cfc 错误?

应用设置如下:

<cfapplication name="MyDataSourceName" 
           sessionmanagement="Yes"
           setclientcookies="yes"
           setdomaincookies="yes"
           loginstorage="session"
           sessiontimeout="#CreateTimeSpan(0, 2,0,0)#">

CustomerProfile.cfc

<cfcomponent>
    <cffunction name="init">
        <cfreturn this> 
    </cffunction>

    <cffunction name="getContactCustomerProfileDetail" returntype="query"         
            description="Returns customer contact details by contactid" 
            access="public">
        <cfargument name="ccId" type="numeric" required="yes"> 

        <cfquery name="getContactCustomerProfileDetail" 
                  datasource="#Application.ds#" 
                  dbtype="ODBC" 
                  username="#Application.UserName#" 
                  password="#Application.Password#">
            <!-------My query here--->
        </cfquery> 

        <cfreturn getContactCustomerProfileDetail>

    </cffunction>

</cfcomponent>  

【问题讨论】:

  • 如果session.userid 为空怎么办?您的 CFC 会返回第一条(或全部)customerprofile 记录吗?
  • 你有Skyhook的代码getContactCustomerProfileDetail()
  • 听起来 CFC 中没有适当的范围。
  • 请贴出customerprofile.cfc的代码
  • 如果 session.userid 为空,系统将注销。

标签: coldfusion cfc application.cfm


【解决方案1】:

正如亚当所说,你需要这样做:-

<cffunction name="getContactCustomerProfileDetail" returntype="query"         
        description="Returns customer contact details by contactid" 
        access="public">
    <cfargument name="ccId" type="numeric" required="yes">

    <cfset var getContactCustomerProfileDetail = false>

    <cfquery name="getContactCustomerProfileDetail" 
              datasource="#Application.ds#" 
              dbtype="ODBC" 
              username="#Application.UserName#" 
              password="#Application.Password#">
        <!-------My query here--->
    </cfquery> 

    <cfreturn getContactCustomerProfileDetail>

</cffunction>

您遇到问题的原因是您的 CFC 实例位于共享范围(应用程序)中,并且您没有对查询变量进行 var'd。这意味着它被设置到 CFC 实例的变量范围内。这意味着多个线程可以覆盖这个值。只需像我展示的那样对变量进行 varring,您就可以将变量设置为函数的本地变量,因此每次调用该函数都会创建一个本地化的并且因此是线程安全的变量。

基本上你应该习惯性地改变函数中的所有局部变量。在我工作过的任何地方,这段代码都无法通过代码审查。

【讨论】:

  • 好吧,我做到了,但就像下面一样 - 它也应该可以工作???
  • 可以的。只是你必须在 CF 中分配一个变量。所以一切都很好,因为 CFQUERY 行会覆盖它。
【解决方案2】:

您实际上并没有包含代码的相关位来回答这个问题...这将是 getCustomerProfileDetail() 中的代码。

但是我假设您没有将所有变量都 VAR 化,这意味着它们进入 CFC 的变量范围,该范围与应用程序中的每个用户共享。

但是,正如我所说,您没有向我们提供正确的信息来真正准确地回答这个问题。我建议更新您的问题以包含相关代码。

【讨论】:

  • 在上面添加了 cfc 代码。请检查它是否会帮助您提出建议
  • 未正确 var'd 的变量是 getcontactcustomerprofiledetail。
  • 我不确定您还希望我说什么。正如我猜想的那样,你没有改变你的变量。所以...嗯... VAR 它
猜你喜欢
  • 2019-09-13
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 2014-05-21
  • 1970-01-01
  • 2011-02-21
相关资源
最近更新 更多