ColdFusion - 捕获登录日期/时间和注销日期/时间

ColdFusion - Capture Login Date / Time and Logout Date / Time

我想知道是否有办法在会话超时时捕获用户 ID。如果他们单击注销按钮,我可以捕获。我只想在他们的会话超时时能够捕获。

在下面添加了这个

<cffunction name="onSessionEnd" access="public" returntype="void">
    <cfquery name="logout" datasource="#application.datasource#">
        update user 
        set logout_date = CURRENT_TIMESTAMP
        where profile_id = #session.user_id#
    </cfquery>
    <cfreturn true />
</cffunction>

所以我做了上面的 session超时的时候没有记录登出日期

所以不得不将应用程序变量更改为会话变量,只有一个对我有用。

<cffunction name="onSessionEnd" access="public" returntype="void">
    <cfquery name="logout" datasource="#session.datasource#">
        update user 
        set logout_date = CURRENT_TIMESTAMP
        where profile_id = #session.user_id#
    </cfquery>
    <cfreturn true />
</cffunction>

添加此评论作为答案以获得更好的格式
利当之无愧。如果你添加你的答案,我会删除这个有利于你的答案。

鉴于您刚刚添加到答案中的示例,您做错了。正如 Leigh 在他的评论中指出的那样,您需要在 OnSessionEnd 方法中以不同方式引用会话范围。

您还缺少 OnSessionEnd 方法的参数。

您可能还想确保变量在尝试使用之前已定义。

您还应该在查询中使用 <cfqueryparam>

把它们放在一起应该看起来像这样:

<cffunction name="OnSessionEnd" access="public" returntype="void" output="false">
    <cfargument name="SessionScope" type="struct" required="true" />
    <cfargument name="ApplicationScope" type="struct" required="false" default="#StructNew()#" />

    <cfif StructKeyExists(ARGUMENTS.ApplicationScope,"datasource") AND StructKeyExists(ARGUMENTS.SessionScope,"user_id")>

        <cfquery name="local.logout" datasource="#ARGUMENTS.ApplicationScope.datasource#">
            update user 
            set logout_date = CURRENT_TIMESTAMP
            where profile_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#ARGUMENTS.SessionScope.user_id#" />
        </cfquery>

    <cfelse>

        <!--- variables are not defined, do something else here? --->

    </cfif>

    <cfreturn />
</cffunction>

此外,请求范围在此方法中不可用,因为它不是由请求调用的。