在cf中防止多次登录最简单的方法是什么?
<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>发布于 2010-11-03 21:56:08
在我工作的地方,他们对限制单个用户登录到单个应用程序的次数有严格的要求。我是这样解决这个问题的:
1)有一个保存用户信息的用户登录表和一个名为“上次更新”和“登录”的字段。当用户使用当前时间日期登录“上次更新”,而“登录”使用1时。这会阻止该用户再次登录。
2)创建一个jquery ajax调用,用新的时间日期戳更新数据库字段last update。这是按计划进行的(比如每1分钟)。
3)我必须做的最后一件事是安排一个任务,检查活动登录(在数据库中)的最后一次更新是否大于固定的时间段,如果用户“已登录”的状态从1变为0(允许他们再次登录)。这很重要的原因是,您不能假设用户在离开时总是单击“注销”。有时他们只是关闭他们的浏览器或导航离开。当发生这种情况时,如果他们试图返回到应用程序,他们将无法再次登录(因为数据库认为他们已经登录)。
希望这能让你走上正确的方向。
发布于 2010-11-03 23:58:07
我找到的最简单的方法是绑定到ColdFusion中的底层java会话作用域,并查找现有的登录名。
如果它在那里,踢出/终止另一个会话,并让当前会话进入。
下面是我的代码中的一个示例。请注意,有不同的方法可以终止/结束会话,每种方法都有自己的优缺点。此示例适用于继承的代码库。
getLogin.fld_userid是html表单中的尝试登录ID。
在初始化会话变量之前,我让这段代码在"on login“期间运行。
<!---code for 1user per login --->
<cfset liveSessions = createObject("java","coldfusion.runtime.SessionTracker")>
<cfset activesessions = liveSessions.getSessionCollection(YOUR_DATA_SOURCE_NAME)>
<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;
}
}
}
</cfscript>
</cfloop>发布于 2019-09-07 21:23:49
从他们的回答中,我假设评论技术并没有达到他们当时想要的效果(2010)。
根据一些谷歌搜索,会话方法(来自底层java) setMaxInactiveInterval可能不再工作(就像使用Lucee5和MySQL数据库进行会话存储一样)。
我尝试了很多方法来清除会话,比如structClear,将会话设置为{},将会话设置为裸属性{cfid, cftoken...},这些都不起作用。
唯一起作用的就是设置或更改会话的各个属性,比如thisSession.loggedIn。
它的用途可能包括允许用户查看和控制他们登录的其他地方。
<cfscript>
liveSessions = createObject("java","coldfusion.runtime.SessionTracker");
activeSessions = liveSessions.getSessionCollection(application.applicationName);
for (s in activeSessions) {
thisSession = activeSessions[s];
if (!StructIsEmpty(thisSession)) {
// Change loggedIn to whatever property identifies a login.
if (isDefined("thisSession.loggedIn") && thisSession.loggedIn &&
thisSession.UserID == form.UserID) {
// Change the UserID line above to whatever match you're looking for
if(thisSession.UserID == form.UserID) {
// Change loggedIn to whatever property identifies a login.
lock scope="session" timeout="5" {
thisSession.loggedIn = 0;
}
break;
}
}
}
}
</cfscript>https://stackoverflow.com/questions/4082991
复制相似问题