Session Info - New Tools for Session Scope Lovers

Tools of the Trade , ColdFusion Add comments

Remember client scope. Ahhh yes, ye olde client scope. I remember 7 years ago, when I stopped using client scope all together, I missed having variables such as client.lastVisit, client.hitCount, and client.TimeCreated. There are several solutions to mimic the first two, but I never found anything that I felt really truly suited me. I've had enhancement requests with Adobe since CF7 days to add this functionality to the session scope, and I remember once when Ray and I barraged Macromedia about it.

Lately I started playing with a java taglib that provided some of the functionality I was looking for. More info on the taglib can be found @ http://www.servletsuite.com/servlets/sessinfotag.htm. Immediately I saw its uses.

Again, I've only quickly played with this, but it looks promising. I created a directory off the webroot. I downloaded the JAR and TLD file there. (Probably don't need the TLD file.)

The JAR exposes 4 methods:

  1. sessionUsedTime: Which answers the question: How long has it been since this session was created? From that you could also extract exact time it was created similar to client.timeCreated.
  2. sessionInactiveTime: ** THE MOST USEFUL! ** Which answer the question: How long since the session was touched? Similar to client.lastVisit.
  3. isNewSession: Self explanatory.
  4. isNotNewSession: Self explanatory.

Let's explore numbers 1 and 2 above. Take a look at this code.

<cfimport taglib="sessinfotag.jar" prefix="random"> 
<random:sessionUsedTime formatted="true" /> 
<random:sessionInactiveTime formatted="true" />

That's it. Hit refresh, and watch the "time created" and "last visit" equivalent counts go up. Very easy and useful.

Advantages:

  • Native JAVA session integration
  • Less code - Just a couple lines
  • Support for multiple windows in the browser (which other code solutions have a hard time with)

Enjoy!

7 responses to “Session Info - New Tools for Session Scope Lovers”

  1. Robert Burns Says:
    Actually should be able to get most of this without the need for the jar/tld. Do a cfdump of the following and check out the methods contained:

    #getPageContext().getSession()#

    Some of the methods are:
    getCreationTime()
    getLastAccessedTime()
    getMaxInactiveInterval()
    isNew()
    getId()
  2. Sami Hoda Says:
    I believe there is an issue with using these. If you reference them, it actually changes their values. I'll try and look up what I found while working on this a few years back.
  3. Sami Hoda Says:
    See: http://jehiah.cz/archive/extended-operations-on-coldfusion-sessions (and related links)
  4. Robert Burns Says:
    Just checked the java files from servletsuite jar referenced above. They are in fact using getPageContext().getSession(), so we should be okay using it as well.

    From their SessionTTLTAG...

    HttpSession httpsession = pageContext.getSession();
    long l = 0L;
    if(null != httpsession)
    l = (long)(1000 * httpsession.getMaxInactiveInterval()) - (System.currentTimeMillis() - httpsession.getLastAccessedTime());
  5. Sami Hoda Says:
    @Robert,

    That's really interesting. I'll have to go back and compare and see if it makes a difference. This should be a good test with CF8.
  6. Danny Armstrong Says:
    Are there any follow ups to this post?

    The 4th comment would seem to invalidate this approach - it would suffer the same results as accessing it directly, which of course resets the values.

    Any thoughts on how it works, if it does?

    Guess I'll have to give it a shot anyway.
  7. watch smallville Episodes Says:
    You seem to have got the niche from the root, Awesome work

Leave a Reply

Leave this field empty:



Powered by Mango Blog. Design and Icons by N.Design Studio