Skip to main content
Inspiring
July 16, 2009
Question

Struct and Session

  • July 16, 2009
  • 3 replies
  • 1315 views

Hi all, i need to store all the screen_code and APP_PRIVILEGES into session struct...but somehow when i dumped "session.AdminPrivStruct" I only get one key instead of getting 17 keys and values? Any reason for that?

   ---------

    <cfquery datasource="#application.orc#" name="GetAdminPriv">
      Select
        p.APP_PRIVILEGES,
        p.SCREEN_CODE,
        sc.SCREEN_NAME
      from
        OPS$RMS.REF_LOGIN a,
        OPS$RMS.LOGIN_PRIVILEGE p,
        OPS$RMS.REF_SCREEN sc
      where
        a.EID = p.EID
        And p.screen_code = sc.screen_code
        And upper(a.EID) = upper('#GetAdminLogin.EID#')
    </cfquery>

<cfoutput query="GetAdminPriv">
     <cfscript>
    session.AdminPrivStruct   = structnew();
    session.AdminPrivStruct["#SCREEN_Code#"] = SCREEN_code;
    session.AdminPrivStruct["#SCREEN_code#_Priv"] = APP_PRIVILEGES;
      </cfscript>
      #SCREEN_code#
     </cfoutput>

This topic has been closed for replies.

3 replies

Inspiring
July 17, 2009

You can, of course, put (almost) any sort of struct into the Session.

The technique that I use is that the session is always set-up in the menu screen of the application (because I know that this will in all cases reliably convey the session-cookie to the client).  And I always have:  <cfset Session.exists = "Y">.

The other screens, then, do (within a <cflock>):  <cfset SessionExists = StructKeyExists(Session, "exists")>.  Then, outside of the lock, we test SessionExists, and if it is not True, <cflocation> to the menu screen.

The session-initialization logic in the menu-screen uses the same technique to check for the existence of a session.  If the session-data needs to contain a structure, it's initialized (with StructNew()) at that time.  (Not that you have to do that, but I do.)

ilssac
Inspiring
July 16, 2009
<cfoutput query="GetAdminPriv">
     <cfscript>
    session.AdminPrivStruct   = structnew();
    session.AdminPrivStruct["#SCREEN_Code#"] = SCREEN_code;
    session.AdminPrivStruct["#SCREEN_code#_Priv"] = APP_PRIVILEGES;
      </cfscript>
      #SCREEN_code#
</cfoutput>

You have the session.AdminPrivStruct   = structnew(); line inside the <cfoutput query=""> loop.  That means each and very iteration through the loop will RESET session.AdminPrivstruct to a new, empty structure.

Try This.

<cfscript>
     session.AdminPrivStruct   = structnew();
</cfscript>
<cfoutput query="GetAdminPriv">
     <cfscript>
    session.AdminPrivStruct["#SCREEN_Code#"] = SCREEN_code;
    session.AdminPrivStruct["#SCREEN_code#_Priv"] = APP_PRIVILEGES;
      </cfscript>
      #SCREEN_code#
</cfoutput>

Or maybe even just this.

<cfset session.AdminPrivQry = getAdminPriv>

Depends on if you really need a structure or just the record set data in a persistant scope.

emmim44Author
Inspiring
July 17, 2009

How will i check for the key values of the struct? Please provide sample...I am getting error from this statement [ <cfif structkeyexists(session.AdminPrivStruct["Admin"],"Admin")>yes<cfelse>no</cfif>] ....

Inspiring
July 17, 2009

Unless AdminPrivStruct contains an element named "Admin" which is itself a structure ... and you are asking CF to see if that structure contains an element which is named "Admin" ... then what you wanted to say is simply:

<cfif StructKeyExists(Session, "AdminPrivStruct") >

This is asking:  "Does the struct named Session ... which, by the way, you know does exist ... contain an element named 'AdminPrivStruct' ?"

You then might wish to use StructKeyExists a second time to see if that structure-element contains the key, "Admin."  Only then is it safe to test its value.

Inspiring
July 16, 2009

<cfoutput query="GetAdminPriv">
     <cfscript>
    session.AdminPrivStruct   = structnew();

    ....
      </cfscript>
</cfoutput>

You are creating a new structure on each iteration, overwriting any previous results each time. The end result is only one key. You need to initialize the structure before the loop.