注销并终止会话

时间:2014-07-03 05:24:58

标签: session coldfusion coldfusion-9 logout coldfusion-10

我有一个工作正常的登录页面。现在我想退出。

以下是我的header.cfm文件中的链接。如果会话变量为true,则显示“注销”。如果没有,则显示“登录”。所以我只想注销。

<a id="login-link" href="login.cfm">
     <cfif session.userLoggedIn>logout <cfelse>LogIn</cfif>
</a>

的Application.cfc

public boolean function onRequestStart(string targetPage)
{

    if(findNocase("login.cfm", arguments.targetPage))
    {
        return true;
    }
    else if(session.userLoggedIn)
        return true;
    else
    {
        include "login.cfm";
        return false;
    }
}

public void function onSessionStart(struct sessionObj)
{

    session.userLoggedIn = false;
}

logIn.cfm

<cfif isDefined("form.btn_login") >
    <cfset userResultResponse =  communtiyServic.getUsers(form.user,form.pwd)>
    <cfset userQry = userResultResponse.getQryData() >

        <cfif userQRY.recordCount gt 0 >
            <cfset session.userLoggedIn = true />
            <cflocation url="index.cfm" >
        <cfelse>
            <cfoutput>invaled userName or password </cfoutput>
    </cfif>



</cfif>

2 个答案:

答案 0 :(得分:6)

如果您使用CFIDCFTOKEN作为会话识别Cookie(您可能不应该这样,但它是默认值),那么您应该能够在logout()方法中拨打SessionInvalidate()。这将使服务器和客户端之间的会话连接无效。我不确定它是否会使服务器上的会话数据到期,但如果没有,它会在超时时间后自动超时。同时它在客户端无法访问,无论出于什么意图和目的,它都能满足您的需求。

答案 1 :(得分:3)

编辑以后修改verbiage的500次编辑是示例代码的答案...我做了一些更改以简化过程并使用url查询字符串从任何页面注销。还有其他方面,但这可以与OPs示例一起使用。

在你的onRequestStart()中添加一些代码来寻找logout / redirecting以使用location()到登录页面。

OP使用现有代码实现注销/重定向的简便方法就是添加onRequestStart():

param name="url.logout" default=0; 
if (isDefined('url.logout') and url.logout) {
    if (isDefined('session')){
        /* 
        You can use structDelete(session,'whatever') 
        if you know the session.whatever you are clipping
        and you will have to loop and kill all SO 
        try the structClear() function below.

        */
        structClear(session); 
        /*
        The OP can redirect to login.cfm
        which will auto take them to the login.cfm page
        provided you tack on the ?logout=1 to the URL like this
        http://yoursite.com/somepage.cfm?logout=1
        */
        location(url="login.cfm"); 
    }
}
相关问题