2010-11-02 86 views
2

防止cf中多次登录的最简单方法是什么?防止在Coldfusion中同时登录

<cfcomponent> 
<cfset This.name = "Name"> 

<cfset This.Sessionmanagement="True"> 
<cfset This.loginstorage="session"> 
<cfset This.sessionTimeout = "#CreateTimeSpan(0, 0, 50, 0)#"> 
<cfset This.applicationtimeout="#createtimespan(0,0,50,0)#"> 
<cfset This.scriptProtect = "All"> 

<cffunction name="OnRequestStart"> 
    <cfargument name = "request" required="true"/> 

    <cfif IsDefined("Form.logout")> 

     <cflogout> 

     <cfset StructClear(Session)> 

     <cflocation url="/" addtoken="no"> 

    </cfif> 

    <cflogin> 
     <cfif NOT IsDefined("cflogin")> 
      <cfinclude template="loginform.cfm"> 
      <cfabort> 


     <cfelse> 
      <cfif cflogin.name IS "" OR cflogin.password IS ""> 
       <cfoutput> 
        You must enter text in both the User Name and Password fields. 
       </cfoutput> 
       <cfinclude template="loginform.cfm"> 
       <cfabort> 
      <cfelse> 
       <cfquery name="loginQuery" dataSource="datadsn"> 
       SELECT * 
       FROM userlogin 
       WHERE 
       UserID = <cfqueryparam value="#cflogin.name#" cfsqltype="CF_SQL_VARCHAR"> 
       AND Password = <cfqueryparam value="#hash(cflogin.password)#" cfsqltype="CF_SQL_VARCHAR"> 
       AND trenabled = <cfqueryparam value="1" cfsqltype="CF_SQL_VARCHAR"> 
<!--- Project ID---> 
       AND projectid = <cfqueryparam value="23" cfsqltype="CF_SQL_VARCHAR"> 
       </cfquery> 

       <cfset loginpass = #hash(cflogin.password)#> 

       <cfset comparison = Compare(loginQuery.Password, loginpass)> 

       <cfif loginQuery.trRoles NEQ "" AND comparison EQ 0> 

       <cfloginuser name="#cflogin.name#" Password = "#loginpass#" roles="#loginQuery.trRoles#"> 

       <cfelse> 
        <cfoutput> 
         Your login information is not valid.<br> 
         Please Try again. 
        </cfoutput>  
        <cfinclude template="loginform.cfm"> 
        <cfabort> 
       </cfif> 
      </cfif>  
     </cfif> 
    </cflogin> 

    <cfif GetAuthUser() NEQ ""> 
     <cfoutput> 
      <form method="Post"> 
       <input type="submit" Name="Logout" value="Logout"> 
      </form> 
     </cfoutput> 

    </cfif> 

</cffunction> 

<cffunction name="onSessionEnd" returnType="void"> 


</cffunction> 

</cfcomponent> 
+1

简短的回答:使用的应用范围。长答案:不! – Henry 2010-11-02 23:51:33

回答

1

在我工作的地方,他们对限制单个用户登录到单个应用程序的数量有严格的要求。我遇到了这样的问题:

1)有一个用户登录表,其中包含用户信息和一个名为“last update”和“Logged in”的字段。当用户使用当前时间日期登录“最近更新”并使用“登录”登录时,这会阻止该用户再次登录。

2)创建一个jquery ajax调用,用新的时间日期戳记更新数据库字段“上次更新”。这发生在一个时间表上(如每1分钟)。

3)我必须做的最后一件事是安排一个任务,检查活动登录的最后更新(在数据库中)是否大于固定时间段,如果是用户“登录”状态从1更改为0(允许他们再次登录)。这一点很重要的原因是你不能假设用户在他们离开时总是会点击“注销”。有时他们只是关闭浏览器或走开。当这种情况发生时,如果他们试图回到应用程序,他们将无法再次登录(因为数据库认为他们已经登录)。

希望这能让你朝着正确的方向前进。

1

我发现这样做的最简单方法是绑定到ColdFusion中的基础java会话作用域中,并为您拥有的那个寻找现有登录名。

如果它的存在,踢出/到期的其他会话,并让当前1英寸

下面是我的代码的例子。请注意,有不同的方式杀死/结束每个会议都有自己的优点/缺点。这个例子是针对一个继承的代码库。

getLogin.fld_userid是来自html表单的尝试登录ID。

我在初始化会话变量之前在“登录时”运行此代码。

<cfset liveSessions = createObject("java","coldfusion.runtime.SessionTracker")> 

<cfloop item="loopSession" collection="#activesessions#"> 
<cfscript> 
thisSession = activesessions[loopSession]; 



    if(StructIsEmpty(thisSession)) 
    { 

    // do nothing 

    } 
    else 
    { 

     if(isDefined("thisSession.loginUserid")) 
     { 

      thisUser = thisSession.loginuserid; 

      if(thisSession.loginuserid EQ getlogin.fldUser_ID) 
      { 
       thisSession.XXAutoToken=""; 
       //structClear(thisSession); 
       //cflocation(theUrl:"index.cfm?home.welcome"); 
       thisSession.setMaxInactiveInterval(1); 
       break;   
      } 
     } 

    }