如何检查会话变量是否已设置?

2my*_*lie 1 coldfusion session-variables coldfusion-10

在检查我的详细信息页面中是否设置了会话变量时,我不断收到“自定义脚本模块中的错误”。即使没有设置会话变量,这个语句不是应该给我真或假而不是错误吗?

<cfif StructKeyExists(session.mysiteShibboleth, "isAuthenticated") and (session.mysiteShibboleth.isAuthenticated) >
    <cflog text="Session-Defined-5: isAuthenticated" type="Information" file="Authentication">
<cfelse>
    <cflog text="Session-Defined-7: It's not authenticated'" type="Information" file="Authentication">
</cfif>
Run Code Online (Sandbox Code Playgroud)

在我的 authenticate.cfm 文件中,这是设置会话变量的地方。

<cfif cgiReferer eq shibboleth_url>
    <cfscript>
        session.mysiteShibboleth = StructNew();
        session.mysiteShibboleth.username=REReplace(http_header.headers.eppn, "@mysite.com","","ALL");
        session.mysiteShibboleth.mail=http_header.headers.eppn;
        session.mysiteShibboleth.groups=ArrayToList(REMatch('WEB\.[A-Z.-]+', http_header.headers.member));
        session.mysiteShibboleth.isAuthenticated="true";
    </cfscript>
</cfif>
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下方法,但仍然出错。我已经阅读了这个线程,它似乎没有解决我的问题。

<cfif IsDefined("session.mysitecShibboleth.isAuthenticated")>
Run Code Online (Sandbox Code Playgroud)

Tim*_*sko 5

session.mysiteShibboleth在检查 上的密钥之前,您需要确保它存在mysiteShibboleth。这很可能是您问题的根源,但如果您向我们提供收到的实际错误消息,我们可以更好地为您提供帮助。

另外,请注意变量名称中<cfif IsDefined("session.mysitecShibboleth.isAuthenticated")>有错误c

** 编辑:添加代码示例

<cfif StructKeyExists(session, "mysiteShibboleth">
    <cfif StructKeyExists(session.mysiteShibboleth, "isAuthenticated") and (session.mysiteShibboleth.isAuthenticated) >
    . . . 
    </cfif>
<cfelse>
    . . . 
</cfif>
Run Code Online (Sandbox Code Playgroud)