没有CFWheels的会话没有坚持

时间:2015-07-05 13:10:45

标签: session coldfusion cfwheels

我在创建会话值时遇到困难。默认值始终存在。以下是我的基本登录工具。

compare函数工作正常,但在cflock和redirect之后,session.userId和session.isLoggedIn分别为0和false。

配置/ app.cfm

<cfset this.name = "xxx">
<cfset this.sessionManagement = true />
<cfset this.sessionTimeout= createtimespan(0,2,0,0) />
<cfset this.setClientCookies = false />
<cfset this.datasource = "xxx" />

事件/ onRequestStart.cfc

<cfscript>
    if ( !StructKeyExists(session, "userId") ) {
        session.userId = 0;
        session.isloggedIn = false;
    }
</cfscript>

控制器/ admin.cfc

<cfcomponent extends="Controller">
    <cffunction name="init">

    </cffunction>

    <cffunction name="login">

    </cffunction>

    <cffunction name="main">

    </cffunction>

    <cffunction name="login_proc">
        <cfset local.userName = "xxx" />
        <cfset local.password = "yyy" />

        <cfif isPost() and StructKeyExists(params, "username")>
            <cfif compare(params.username, local.username) eq 0 AND compare(params.password, local.password) eq 0>

                <cflock scope="Session" type="exclusive" timeout="3">
                  <cfset session.userId = local.userName />
                  <cfset session.isLoggedIn = true />
                </cflock>

                <cfset redirectTo(action="main")>
            <cfelse>
                <cfset
                    flashInsert(
                        error_msg="Incorrect login."
                )>
                <cfset redirectTo(action="login")>
            </cfif>
        <cfelse>
            <cfset redirectTo(action="login")>
        </cfif>
    </cffunction>
</cfcomponent>

视图/管理/ login.cfm

<cfform action="/-rootdir-/index.cfm/admin/login_proc" method="post">
        <p>
            <label for="login">Username</label>
            <cfinput type="text" name="username" size="20" required="yes" message="Enter your username" autofocus />
        </p>
        <p>
            <label for="password">Password</label>
            <cfinput type="password" name="password" size="20" required="yes" message="Enter your password" />
        </p>

        <input type="submit" name="login" value="Go" />
    </cfform>

1 个答案:

答案 0 :(得分:2)

config/app.cfm中的这一行造成了麻烦:

<cfset this.setClientCookies = false />

setClientCookies设置cfidcftokenjsessionid等方式。不会将Cookie传递给客户端,而ColdFusion没有信息关于重定向发生后客户端的会话。

如果您绝对必须使用此设置,则需要在所有重定向和链接中传递cfidcftoken

<cfset redirectTo(action="main", addToken=true)>

<!--- Can't use linkTo anymore unless you override `urlFor` in the controller to use ColdFusion's built-in UrlSessionFormat() function --->
<a href="#UrlSessionFormat(urlFor(action='main'))#>My link</a>

我怀疑你是否想要这种行为。