如果session.allowin不为true,则使所有页面从index.cfm开始

时间:2016-05-17 15:57:51

标签: coldfusion

我正在使用coldfusion 10申请,我正在试图弄清楚如何让每一页都进入我的"登录页面"我现在已经设置为index.cfm。

无论您打开哪个页面将其默认为登录页面,但是由于某种原因页面无法正确打开,网址都是正确的。

非常感谢任何帮助!

的Application.cfc

<cfcomponent>
    <cfparam name="session.allowin" default="false">
    <cfparam name="session.userid" default="0">
    <cfif session.allowin neq "true">   
          <cflocation url="index.cfm">
    </cfif>

    <cfset this.datasource = "database" >
    <cfset this.name = "ExitInterview">
    <cfset this.sessionManagement = "true">
    <cfset this.sessionTimeout = "#createTimeSpan(0,5,0,0)#">
    <cfset this.clientManagement = "false">
    <cfset this.loginStorage = "session">
    <cfset this.setDomainCookies = "true">
    <cfset this.scriptProtect = "true">
    <cfset this.applicationTimeout = "#createTimeSpan(0,5,0,0)#">
</cfcomponent>

Browser - Page is not redirecting properly

更新 根据答案,这是我最新的Application.cfc,它仍然没有正确重定向:

<cfcomponent
displayname="ExitInterview"
output="true"
hint="Handle the application.">


<!--- Set up the application. --->
<cfset this.datasource = "database" >
<cfset this.name = "ExitInterview">
<cfset this.sessionManagement = "true">
<cfset this.sessionTimeout = "#createTimeSpan(0,5,0,0)#">
<cfset this.clientManagement = "false">
<cfset this.loginStorage = "session">
<cfset this.setDomainCookies = "true">
<cfset this.scriptProtect = "true">
<cfset this.applicationTimeout = "#createTimeSpan(0,5,0,0)#">


<!--- Define the page request properties. --->
<cfsetting
    requesttimeout="20"
    showdebugoutput="false"
    enablecfoutputonly="false"
    />


<cffunction
    name="OnApplicationStart"
    access="public"
    returntype="boolean"
    output="false"
    hint="Fires when the application is first created.">

    <!--- Return out. --->
    <cfreturn true />
</cffunction>


<cffunction
    name="OnSessionStart"
    access="public"
    returntype="void"
    output="false"
    hint="Fires when the session is first created.">

    <cfset session.allowin = false />
    <cfset session.userid = 0 />
    <!--- Return out. --->
    <cfreturn />
</cffunction>


<cffunction
    name="OnRequestStart"
    access="public"
    returntype="boolean"
    output="false"
    hint="Fires at first part of page processing.">

    <!--- Define arguments. --->
    <cfargument
        name="TargetPage"
        type="string"
        required="true"
        />

    <!---  Set an array of pages where the user does not need to be logged in. --->
    <cfset local.aNonLoggedInPages = ["index.cfm", "login_process.cfm"] />
    <cfset local.bRedirect = true />

    <!---  If you're not logged in, check if you should redirect from the currect page. --->
    <cfif !session.allowin>
        <cfloop array="#local.aNonLoggedInPages#" index="local.x">
            <cfif local.x EQ arguments.targetPage>
                <cfset local.bRedirect = false />
            </cfif>
        </cfloop>

        <cfif local.bRedirect>
            <cflocation url="index.cfm" />
        </cfif>  
    </cfif>

    <!--- Return out. --->
    <cfreturn true />
</cffunction>


<cffunction
    name="OnRequest"
    access="public"
    returntype="void"
    output="true"
    hint="Fires after pre page processing is complete.">

    <!--- Define arguments. --->
    <cfargument
        name="TargetPage"
        type="string"
        required="true"
        />

    <!--- Include the requested page. --->
    <cfinclude template="#ARGUMENTS.TargetPage#" />

    <!--- Return out. --->
    <cfreturn />
</cffunction>


<cffunction
    name="OnRequestEnd"
    access="public"
    returntype="void"
    output="true"
    hint="Fires after the page processing is complete.">

    <!--- Return out. --->
    <cfreturn />
</cffunction>


<cffunction
    name="OnSessionEnd"
    access="public"
    returntype="void"
    output="false"
    hint="Fires when the session is terminated.">

    <!--- Define arguments. --->
    <cfargument
        name="SessionScope"
        type="struct"
        required="true"
        />

    <cfargument
        name="ApplicationScope"
        type="struct"
        required="false"
        default="#StructNew()#"
        />

    <!--- Return out. --->
    <cfreturn />
</cffunction>


<cffunction
    name="OnApplicationEnd"
    access="public"
    returntype="void"
    output="false"
    hint="Fires when the application is terminated.">

    <!--- Define arguments. --->
    <cfargument
        name="ApplicationScope"
        type="struct"
        required="false"
        default="#StructNew()#"
        />

    <!--- Return out. --->
    <cfreturn />
</cffunction>


<cffunction
    name="OnError"
    access="public"
    returntype="void"
    output="true"
    hint="Fires when an exception occures that is not caught by a try/catch.">

    <!--- Define arguments. --->
    <cfargument
        name="Exception"
        type="any"
        required="true"
        />

    <cfargument
        name="EventName"
        type="string"
        required="false"
        default=""
        />

    <!--- Return out. --->
    <cfreturn />
</cffunction>

1 个答案:

答案 0 :(得分:4)

首先,我建议您了解有关Application.cfc的更多信息:

http://www.learncfinaweek.com/week1/Application_cfc/

您应该在onSessionStart()中设置会话默认值,而不是在文件的根目录中。

接下来,您的访问权限检查应位于onRequestStart()。应该看起来像这样(未经测试的代码):

<cfcomponent>

    <cfset this.datasource = "database" >
    <cfset this.name = "ExitInterview">
    <cfset this.sessionManagement = "true">
    <cfset this.sessionTimeout = "#createTimeSpan(0,5,0,0)#">
    <cfset this.clientManagement = "false">
    <cfset this.loginStorage = "session">
    <cfset this.setDomainCookies = "true">
    <cfset this.scriptProtect = "true">
    <cfset this.applicationTimeout = "#createTimeSpan(0,5,0,0)#">

    <cffunction name="onApplicationStart" access="public" output="false" returntype="boolean">
        <cfreturn true />
    </cffunction>

    <cffunction name="onSessionStart" access="public" output="false" returntype="void">
        <cfset session.allowin = false />
        <cfset session.userid = 0 />
    </cffunction>

    <cffunction name="onRequestStart" access="public" output="false" returntype="boolean">
        <cfargument name="targetPage" type="string" required="true" />
        <!--- Set an array of pages where the user does not need to be logged in. --->
        <cfset local.aNonLoggedInPages = ["index.cfm", "login_process.cfm"] />
        <cfset local.bRedirect = true />

        <!--- If you're not logged in, check if you should redirect from the currect page. --->
        <cfif !session.allowin>
            <cfloop array="#local.aNonLoggedInPages#" index="local.x">
                <cfif local.x EQ arguments.targetPage>
                    <cfset local.bRedirct = false />
                </cfif>
            </cfloop>

            <cfif local.bRedirect>
                <cflocation url="index.cfm" />
            </cfif>  
        </cfif>

        <cfreturn true />

    </cffunction>

</cfcomponent>

编辑:在检查重定向之前更新以检查您是否已登录。

编辑评论:评论这不是一个糟糕的问题,老实说,这是一个糟糕的方法,你正在尝试做什么。想象一下,你有10个登录页面和20个页面,你不需要登录。然后你添加更多页面,你不需要登录。维护一个不断增长的非登录页面数组不是你想做的。

更好的方法是:


\Application.cfc
\index.cfm
\about_us.cfm
\contact.cfm
\login.cfm
\login_process.cfm
\private\
\private\Application.cfc
\private\index.cfm

在这个结构中,

\Application.cfc与上述内容相同,但没有onRequestStart()中的内容。

\private\Application.cfc扩展\Application.cfc,并且只应包含以下内容(请注意将CF引用到网站根目录的前导斜杠):

<cffunction name="onRequestStart" access="public" output="false" returntype="boolean">
    <cfargument name="targetPage" type="string" required="true" />

    <cfif !session.allowin>
        <cflocation url="\index.cfm" />
    </cfif>

    <cfreturn true />

</cffunction>

现在,对于\private\文件夹中的任何CFM页面,您必须登录才能访问它。您可以在与\private\相同的级别拥有多个文件夹,每个文件夹都有自己的Application.cfc(仍然扩展根目录),每个文件夹都有相同的登录检查。您还可以使用它们来组织与每个文件夹的“子应用程序”相关的会话变量。