2myCharlie
2myCharlie

Reputation: 1897

How do I check if a session variable is set?

I kept getting the "Error in custom script module" when checking if the session variable is set or not in my details page. Even if the session variable not set, wasn't this statement supposed to give me either true or false instead of error out?

<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>

In my authenticate.cfm file, this is where the session variables are set.

<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>

I have also tried the following and it's still error out. I have read this thread and it doesn't seem to resolve my issue.

<cfif IsDefined("session.mysitecShibboleth.isAuthenticated")>

Upvotes: 1

Views: 3957

Answers (1)

Tim Jasko
Tim Jasko

Reputation: 1542

You need to make sure that session.mysiteShibboleth exists before you check for a key on mysiteShibboleth. That is likely the source of your problem, but if you gave us the actual error message received, we could help you better.

Also, note that <cfif IsDefined("session.mysitecShibboleth.isAuthenticated")> has an errant c in the variable name.

** Edit: Adding code example

<cfif StructKeyExists(session, "mysiteShibboleth">
    <cfif StructKeyExists(session.mysiteShibboleth, "isAuthenticated") and (session.mysiteShibboleth.isAuthenticated) >
    . . . 
    </cfif>
<cfelse>
    . . . 
</cfif>

Upvotes: 6

Related Questions