06
Aug
03

Access session vars from outside the session

There are many situations when it is necessary to access a session variable from outside the actual session. You may want an administrative page to list active users or you may want to utilize a scheduled task to process data stored in user’s sessions.

While it’s simple enough to create a sessions structure in the application scope to store all sessions, cleanup becomes an issue and you’re really duplicating work that ColdFusion must be doing internally anyways. After some digging I found that ColdFusion MX has a SessionTracker class that it uses to keep track of all sessions and you can access it to!

The following test code turns on session tracking and then grabs a structure containing all session variables which is dumped to the response.

getSessionCollection is a static function that returns a structure of all sessions for the specified application, which hopefully should be the current application. The sessions structure it returns has keys of in the following form:

applicationName_cfid_cftoken

or

applicationName_jsessionid

Depending on if you’re using CF sessions or J2EE sessions. The structure value is a reference to the session scope, which itself is a structure. Once you have this reference, you can read or manipulate the session as you would with any other structure.


32 Responses to “Access session vars from outside the session”


  1. 1 Behrang Aug 6th, 2003 at 7:03 pm

    Thanks, it was a nice tip!

  2. 2 Jimmy Aug 8th, 2003 at 12:30 pm

    Wow! This is great! Thanks a lot!

  3. 3 Matthew Aug 8th, 2003 at 4:45 pm

    what about this option?

    <cfset keySet = session.keySet()>
    <cfset iterator = keySet.iterator()>

    <cfloop condition=”#iterator.hasNext()#”>
    <cfset key = iterator.next()>
    <cfset aSession = session.get(key)>
    </cfloop>

    which doesnt require accessing and internal classes

    Matt

  4. 4 Sam Aug 11th, 2003 at 1:58 pm

    Matt,

    That’s nice use of the underlying Java code I haven’t seen before. Please note that the code I posted above will grab any session, not just the current session. The code you listed will iterate through all keys in the current session, which can also be done with

    <cfloop item=”key” collection=”#session#”>

    Best regards,

    Sam

  5. 5 Herman Aug 12th, 2003 at 11:35 am

    Would you still be able to access other CF application’s session data if they were running in seperate JVM’s and/or seperate instances of JRun (ala CF for J2EE)?

    In a seperate, but related question, does J2EE have any built in mechanism for sessions shared across clustered environments?

  6. 6 Raymond Camden Aug 12th, 2003 at 2:11 pm

    Mind if I post a UDF version of this to cflib.orb?

  7. 7 Sam Aug 12th, 2003 at 3:58 pm

    Ray, thanks for asking. Yes, if course go ahead and post to cflib. I’ve been meaning to post this and the unzip thing, but I seem to have misplaced my cflib password. Any chance you can add a “forgot your password” feature to cflib? :-)
    I’m not sure a license applies to two lines of code, but if it does then the license at the bottom of or blog in really tiny letter says “Attribution-ShareAlike Creative Commons License” which means copy to your hearts content, but please mention me. :-)

  8. 8 Sam Aug 12th, 2003 at 4:02 pm

    Herman, this method probably would not work across CF applications running in different instances of CF, but I think the valid use cases are primarily for administrative and scheduled stuff.

    The only time I can think of that you’re doing cross-application session grabbing is if you’re doing something naughty.

    Yes, J2EE sessions can be shared across a cluster. Not sure how to set it up though–but I’m certain it’s going to be different for each J2EE server.

  9. 9 Herman Aug 13th, 2003 at 2:45 pm

    I was considering the technique for use in a single-sign-on mechanism. ‘Naughty’ intentions should certainly be a concern, especially if there is an internal mechanism for getting a list of Applications running on a given server, something along the lines of coldfusion.runtime.ApplicationTracker (a fictitious class name, as far as i know).

    But, concerning multiple JVM’s (not to be confused with multiple instances of CF)…It seems that CF will spawn many JVM simultaneously when running…Are you guaranteed to have an application running in only one JVM? I suppose my point is, is this technique guaranteed to give you all current sessions for a given application?

  10. 10 Sam Aug 14th, 2003 at 12:20 pm

    Herman, yes, single-signon is a great use for this. I hadn’t thought of that.

    Regarding multiple JVM, I’d be really surprised if one application was split among several JVM ’cause it would be really hard to implement locking and sessions in that case. My understanding is that each instance of CF had one JVM, but I could be wrong. I’d suggest askign the question in webforums and perhaps you’ll get a more definitive answer from someone with more J2EE experience. If you do, please post the URL here so we can all follow the conversation.

  11. 11 Tim Blair Aug 19th, 2003 at 7:25 am

    The coldfusion.runtime.ApplicationTracker does indeed exist but under the name of ApplicationScopeTracker instead. I have posted an entry on my blog http://tech.badpen.com/ that extends the example given here to selecting all session data across all applications currently running.

    I have also posted a follow up regarding security concerns based on this functionality…

    In addition, I am assuming the use of JRun in-memory session replication will enable the SessionTracker to function across clusterd MX instances. This could be used for quick “how many users online” type functionality across a load-balanced site without the use of a database or other storage mechanism.

  12. 12 Sam Sep 3rd, 2003 at 12:04 am

    Nice find Tim, didn’t know about ApplicationScopeTracker. I agree there is a security issue but Macromedia doesn’t (I checked with them before posting).

  13. 13 Greg Nov 3rd, 2003 at 1:28 pm

    Fantastic tip, thanks for the heads up!

  14. 14 Ravi Nov 3rd, 2003 at 6:26 pm

    Can some one give me the sample code, how to use the above tracker which enables single login at a time(How to loop structure of structures and find the user login id, if so error message)?

    Thanks,
    Ravi

  15. 15 Ravi Nov 3rd, 2003 at 6:27 pm

    Can some one give me the sample code, how to use the above tracker which enables single login at a time(How to loop structure of structures and find the user login id, if so error message)?

    Thanks,
    Ravi

  16. 16 Gorandri Nov 4th, 2003 at 2:54 am

    So if I want to write a message:”There is 10 active users.”, what I have to do? I only need number of active session/users on one application. Thx.

  17. 17 Sam Nov 4th, 2003 at 8:03 am

    Ravi, did you run the above code and see what it dumps to the screen? It will dump a structure of all sessions listing all session variables. To ensure a user only logs into one session at a time loop through those sessions checking the login id and if found, reject second login.

    Gorandri, the getSessionCollection used above returns a structure. Each element in the structure represents one active session. If you’re not familiar with how to obtain a count of elements in a structure please refer to the documentation. http://livedocs.macromedia.com

  18. 18 Ravi Nov 4th, 2003 at 6:27 pm

    Can some one please post the sample code which loops through structure of structures , I mean for a key the value itself is a structure which has again keys? I need to digtown to last structure and find the login code in the dumped session structures.? I don’t where I am missing it is always throwing some exception whenever I am trying to loop the structure inside the structure.

    Any help will be greatly appreciated?

    Thanks,
    Ravi.

  19. 19 tommy Nov 5th, 2003 at 8:14 pm

    any ideas to modify FMX2004 components?

  20. 20 joe Nov 5th, 2003 at 8:15 pm

    Yeah i have some question about that too

  21. 21 Chafic Kazoun Nov 5th, 2003 at 11:07 pm

    Flash MX 2004 components are packaged up into SWC’s. SWC’s simply are a compiled version of a component. There are several benefits to this new method of packaging component but one side affect is that the code is not editable without having the original source (well at least no easily). Macromedia was nice enough to include most of the source code for the components and the source Fla used to produce those swc’s with Flash MX 2004. You can find the classes in your classes folders under classes\mx\controls (most of them are there some are in other folders in the mx folder). The Fla’s used to produce the components are provided in the Flash MX 2004\en\First Run\ComponentFLA. If you want to edit the code you can edit the .as files in the classes folder. I word of advice though. I suggest you make a copy of the files needed to a working directory if you are going to modify the code for the classes directly. I recommend you leave those class files alone as Macromedia has distributed them. Also rather than edit the class code, try to sub-class the existing classes and to achieve your customization. This is a really lengthy subject to cover in a blog comment. Maybe I’ll write a more lengthy post on the subject another time.

  22. 22 Harold Nov 6th, 2003 at 12:39 pm

    This is a great idea. Thanks for bringing it to my attetion.

    I do have one question. I set session.member_id variable before I run the code. I was looking to see if this code displays the session.member_id variable so that way I can loop through and see if the session already exits. When I login using 3 different windows I do not see session.member_id lised in the cfdump display when I thought I would.

    Can anyone help me out with this? I want to check to see if the session.member_id is in use and if it is, want to send the second user to a bad login page.

    Thanks.

  23. 23 subramanian Mar 3rd, 2004 at 9:10 am

    guys,

    i was browisng thru my application and my friend saw the url and started typing the url to some other page and he got the in to the application with out loging in . he used my cftoken&cfid and
    used it to login and access the application.how ever he was able to do it while i have login in.
    if i logout he was not able to do it.i think iam doing some thing wrong with handling sessions or configuration in the server is wrong.

    i hope you guys can help me understand more. incase you need more information please get back to my email address which is rsubu@getgroup.com.

    for past 4 hrs iam going crazy.

    thanks in advance,
    regards

    subbu

  24. 24 Vince Mar 4th, 2004 at 2:22 pm

    I just wanted to thanks Sam and everyone for posting and adding to this thread. This information is very useful.

  25. 25 CL May 12th, 2004 at 11:23 am

    Sam,

    This is interesting. Even if I delete all the keys while in a specific session or do a SructClear(Session) while in a specific session, your sessiontracker code still lists the session as MyAppName_sessionidvalue with an empty structure. Why does this artifact remain even though the session was specifically removed? How long do they last. I opened up several sessions, ended them and yet they all appeared to persist?

    Thanks

  26. 26 CL May 12th, 2004 at 11:23 am

    Sam,

    This is interesting. Even if I delete all the keys while in a specific session or do a SructClear(Session) while in a specific session, your sessiontracker code still lists the session as MyAppName_sessionidvalue with an empty structure. Why does this artifact remain even though the session was specifically removed? How long do they last. I opened up several sessions, ended them and yet they all appeared to persist?

    Thanks

  27. 27 CL May 12th, 2004 at 11:23 am

    Sam,

    This is interesting. Even if I delete all the keys while in a specific session or do a SructClear(Session) while in a specific session, your sessiontracker code still lists the session as MyAppName_sessionidvalue with an empty structure. Why does this artifact remain even though the session was specifically removed? How long do they last. I opened up several sessions, ended them and yet they all appeared to persist?

    Thanks

  28. 28 Pacific Poker Nov 8th, 2004 at 3:10 pm

    To play pacific poker http://www.pacificpoker-online.com pacific poker online poker site is your target. Inside pacific poker website you can find poker tables, information, and friends.

  29. 29 pacific poker Nov 29th, 2004 at 2:01 pm

    Here, now, go pacific poker is just for you. No need for previous experience. Pacific poker http://pacific-poker.p6.org.uk pacific poker the poker leader.

  30. 30 PokerJoe Aug 28th, 2006 at 6:12 pm

    Hi,

    Interesting blog!

    I thought I wanted to contribute with a cool site: http://poker365.info - it has got pokernews from all the big sites, and no ads!

    Enjoy ;-)

  31. 31 mac Jan 18th, 2007 at 4:46 am

    I thought I wanted to contribute with a cool site:

    http://www.universityofnorthtexas.info/

    Thanking you!!!!!

  1. 1 pacific poker Trackback on Jun 2nd, 2005 at 6:27 pm
Comments are currently closed.