Get A List of Current Sessions

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Get A List of Current Sessions

    I am writing an application and I would like to be able to go to a page and get a list of every user that has a current session... Is this possible? Please help!!!!

    Thank you!
    jeby Guest

  2. Similar Questions and Discussions

    1. HOWTO: List current live streams' names
      Hi All, I'd like in my client side action script to list or enumerate all the names of streams currently live within a server application. I...
    2. List all Current Session Variables
      Hi, Is it possible in CF 5 to list all session variables currently assigned on a web site. For instance, if I assign a sesson variable that is...
    3. Change style of a single row of the item list of datagrid, based on a field value of current item...
      Sorry for the long subject guys, but I don't know how better I can resume the matter... Anyway, I have my datagrid showing items of an order. I...
    4. Current List of GUI Toolkits for Ruby???
      Not meaning to open this can of worms all over again, but my searches for a current GUI toolkit list is not being very successful. OTOH, it's...
    5. value list with unique options by current record
      How can I make a value list to which I can add not only to the whole list by checking the possibility to show "edit", but also add options stored...
  3. #2

    Default Get A List of Current Sessions

    I am writing an application and I would like to be able to go to a page and get a list of every user that has a current session... Is this possible? Please help!!!!

    Thank you!
    jeby Guest

  4. #3

    Default Re: Get A List of Current Sessions

    I'd use an Application variable as a list and append data to the list when
    people activate their session. Taking people off the list is little more tricky.

    If its usernames you want to track it could look like this. Some things to
    note:

    - The code example stores usernames as an Application variable which may be a
    secrity risk. You should consider who and when will see this list.
    - I omitted <cflock> tags for brevity, you should use them wehen setting and
    reading application and/or session variables
    - There is no code which removes user's from the list unless they "logout" (I
    assume you have a logout function). Thus if user's session expire because of
    inactivity
    they will continue to remain in the list. A solution would be slightly more
    complex but possible




    Put this in Application.cfm

    <!--- set a default, empty list --->
    <cfparam name="Application.LoggedIn" default="">




    Then on your login page. . .
    <cfif [Start Session logic]>

    <!--- set your session var like normal --->
    <cfset Session.Username = Form.Username>

    <!--- Append the session var to the application var list if it's not already
    in there --->
    <cfif NOT ListFindNoCase(Application.LoggedIn, Session.Username)>
    <cfset Application.LoggedIn = ListAppend(Application.LoggedIn,
    Session.Username)>
    </cfif>

    </cfif>




    Then on your logout page we need to remove the session var from the list

    <!--- find the session position in the list --->
    <cfset listPos = ListFindNoCase(Application.LoggedIn, Session.Username)>

    <!--- if we found it. . . --->
    <cfif listPos>

    <!--- re-set the list with the session var deleted from it --->
    <cfset Application.LoggedIn = ListDeleteAt(Application.LoggedIn, listPos)>

    </cfif>




    And to view the list of people logged in

    <cfif ListLen(Application.LoggedIn)>
    <cfoutput>#Application.LoggedIn#</cfoutput>
    </cfif>

    LeftCorner Guest

  5. #4

    Default Re: Get A List of Current Sessions

    I'm not sure that this would scale well, once your concurrent users
    grow, you will find yourself with a bloated application scope, and
    foget about cfdumping it, you're likely to get a timeout error.

    Tracking sessions is tricky because normal session storage is made on
    a client cookie so, you're not really storing anything on the server
    but you're giving that load to the user. I think that in session
    management in your cfapplication you can set session storage to a
    database, if that's the case you might be able to query the table
    that's holding your sessions.

    On Mon, 6 Jun 2005 23:52:04 +0000 (UTC), "LeftCorner"
    <webforumsuser@macromedia.com> wrote:
    >I'd use an Application variable as a list and append data to the list when
    >people activate their session. Taking people off the list is little more tricky.
    >
    > If its usernames you want to track it could look like this. Some things to
    >note:
    >
    > - The code example stores usernames as an Application variable which may be a
    >secrity risk. You should consider who and when will see this list.
    > - I omitted <cflock> tags for brevity, you should use them wehen setting and
    >reading application and/or session variables
    > - There is no code which removes user's from the list unless they "logout" (I
    >assume you have a logout function). Thus if user's session expire because of
    >inactivity
    > they will continue to remain in the list. A solution would be slightly more
    >complex but possible
    >
    >
    >
    >
    > Put this in Application.cfm
    >
    > <!--- set a default, empty list --->
    > <cfparam name="Application.LoggedIn" default="">
    >
    >
    >
    >
    > Then on your login page. . .
    > <cfif [Start Session logic]>
    >
    > <!--- set your session var like normal --->
    > <cfset Session.Username = Form.Username>
    >
    > <!--- Append the session var to the application var list if it's not already
    >in there --->
    > <cfif NOT ListFindNoCase(Application.LoggedIn, Session.Username)>
    > <cfset Application.LoggedIn = ListAppend(Application.LoggedIn,
    >Session.Username)>
    > </cfif>
    >
    > </cfif>
    >
    >
    >
    >
    > Then on your logout page we need to remove the session var from the list
    >
    > <!--- find the session position in the list --->
    > <cfset listPos = ListFindNoCase(Application.LoggedIn, Session.Username)>
    >
    > <!--- if we found it. . . --->
    > <cfif listPos>
    >
    > <!--- re-set the list with the session var deleted from it --->
    > <cfset Application.LoggedIn = ListDeleteAt(Application.LoggedIn, listPos)>
    >
    > </cfif>
    >
    >
    >
    >
    > And to view the list of people logged in
    >
    > <cfif ListLen(Application.LoggedIn)>
    > <cfoutput>#Application.LoggedIn#</cfoutput>
    > </cfif>
    Ro Guest

  6. #5

    Default Re: Get A List of Current Sessions

    That will work only if people logout correctly though... is there a server
    script or something i could put in a scheduled task that could check and see if
    the people in the list have current sessions and delete them if they dont?

    jeby Guest

  7. #6

    Default Re: Get A List of Current Sessions

    Are you using CF7? If so you can use the OnSessionEnd() functionality in the
    Application.cfc

    Otherwise you can add a last log in field to your DB, then if a member is
    logged in you can update the date and time on every page request. Then to get
    the online members run a query for values within the last 30 minutes!

    Stressed_Simon Guest

  8. #7

    Default Re: Get A List of Current Sessions

    I got code for a session tracker, similar to what you are asking about, from a
    magazine a couple years or so back. I think it was Cold Fusion Developers
    Journal. Anyway, it's pretty simple, and shows me who is online and whether
    they are logged in. Obviously, you can store the data in a table if you want,
    or modify it further.



    <!--- in application.cfm, in a block that executes every time, i.e. not in a
    cfif or such --->

    <cflock timeout="10"
    throwontimeout="Yes"
    type="EXCLUSIVE"
    scope="APPLICATION">
    <cfscript>

    if (NOT IsDefined("Application.SessionTracker") OR NOT
    IsStruct(Application.SessionTracker))
    {
    Application.SessionTracker = StructNew();
    }

    UserInfo = CGI.REMOTE_ADDR;
    if (IsDefined("Cookie.CGCoreFullName") AND Len(Trim(Cookie.CGCoreFullName)))
    {
    UserInfo = Cookie.CGCoreFullName & " on " & CGI.REMOTE_ADDR;
    ModBy = Cookie.CGCoreFullName;
    }
    dummy = StructInsert(Application.SessionTracker, UserInfo, Now(), true);
    </cfscript>

    <!--- in another template, which I call sessionreport.cfm. Note that I haven't
    modified this code much,
    it's pretty much how I got it. --->

    <cfset TheTimeOut = CreateTimeSpan(0,4,0,0)>

    <cfoutput>

    <h3>Live Session-Tracker Report</h3>

    <cflock name="#APPLICATION.applicationName#"
    type="Exclusive"
    timeout="20"
    throwontimeout="Yes">

    <p>
    <table border="2" cellspacing="0" width="95%" class="sm">
    <tr bgcolor="cccccc">
    <td><b>User (by IP-address)</b></td>
    <td><b>Session-Status</b></td>
    </tr>

    <CFLOOP collection="#APPLICATION.SessionTracker#" item="aUser">
    <cfset onlineSince = StructFind(APPLICATION.SessionTracker, aUser)>

    <cfset inactiveSince = DateDiff("n", onlineSince, Now())>

    <!--- I don't remember why this line is commented out! --->
    <!--- <CFIF DateCompare(onlineSince+theTimeout, Now()) EQ 1> --->

    <cfset TheTimeOutInMinutes = DatePart("h", TheTimeOut) * 60>
    <cfif inactiveSince LTE TheTimeOutInMinutes>
    <!--- User's last activity lies within session-timeout, so his/her
    session is active --->

    <!--- The threshold for coloring the report may be set individually:
    --->
    <cfif inactiveSince LTE 2>
    <cfset theColor = "FFFFD2">
    <cfelseif inactiveSince LTE 5>
    <cfset theColor = "Blue">
    <cfelse>
    <cfset theColor = "Cyan">
    </cfif>

    <!--- Output of current user in report --->
    <tr>
    <td bgcolor="eeeeee">#aUser#</td>
    <td bgcolor="#theColor#">inactive since <b>#inactiveSince#</b> mins
    </td>
    </tr>

    <cfelse>
    <!--- User's session has timed-out, so we can delete his IP from the
    Session-Tracker --->

    <cfset dummy = StructDelete(APPLICATION.SessionTracker, aUser)>

    </cfif>
    </cfloop>

    </table>

    <p>

    #StructCount(APPLICATION.SessionTracker)# Users online

    </cflock>
    </cfoutput>

    mkane1 Guest

  9. #8

    Default Re: Get A List of Current Sessions

    Yes I am using CF7....

    Thank you very much for all of your help everyone!

    These forums are the best!
    jeby Guest

  10. #9

    Default Re: Get A List of Current Sessions

    Hey everyone I have a similar deal going on, where I keep a list of currently
    logged on users. I do not understand how that onsessionendtag works. How i tell
    if people are online or not is by holding a value in my database (users) called
    online, and the value is yes or no. Then my index page just looks for all
    entries in the database where online = yes and outputs them under the header of
    usrs logged on. How could I use that onsessionend tag to help me out here?

    kenji776 Guest

  11. #10

    Default Re: Get A List of Current Sessions

    OnSessionEnd() is not a tag but a CFC method in Application.cfc it is only available in ColdFusion 7.

    Are you using CF7?
    Stressed_Simon Guest

  12. #11

    Default Re: Get A List of Current Sessions

    Okay my bad. How to I use this method? And yes I do have cf7.
    kenji776 Guest

  13. #12

    Default Re: Get A List of Current Sessions

    Well basically you have to create a CFC called Application.cfc which replaces
    the Application.cfm.

    Then you create a method called OnSessionEnd, any code you add in there will
    be run when a session expires!

    Stressed_Simon Guest

  14. #13

    Default Re: Get A List of Current Sessions

    forgive my idiocy, but can I copy and paste the code from my current
    aplication.cfm into my aplication.cfc? I know that cfc's are used for something
    different, I don't know if they support regular cold fusion code, along with
    methods and such.

    kenji776 Guest

  15. #14

    Default Re: Get A List of Current Sessions

    Unfortunately it is not as simple as that. If you are not familiar with how to
    use CFCs you are probably going to find this a bit tricky. I'd read up on the
    use of CFCs:-

    [url]http://www.macromedia.com/devnet/mx/coldfusion/extreme/bforta_cfc.html[/url]

    Then once you are happy with that then read up on how to use the
    Application.cfc:-


    [url]http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/[/url]
    wwhelp.htm?context=ColdFusion_Documentation&file=0 0001117.htm

    Stressed_Simon Guest

  16. #15

    Default Re: Get A List of Current Sessions

    Well, if you're querying a dabase that contains client sessions, you
    can ask the return set to contain the sessions that have not been
    closed (assumed open), or the sessions that have inactivity of less
    than the default timeout (e.g. sessions that have activity of less
    than 30 minutes if that's your session/login timeout)


    On Thu, 9 Jun 2005 12:28:09 +0000 (UTC), "jeby"
    <webforumsuser@macromedia.com> wrote:
    >That will work only if people logout correctly though... is there a server
    >script or something i could put in a scheduled task that could check and see if
    >the people in the list have current sessions and delete them if they dont?
    Ro Guest

  17. #16

    Default Re: Get A List of Current Sessions

    I have a question... can I use an Application.cfm and an Application.cfc at the same time or will that cause a problem?
    jeby Guest

  18. #17

    Default Re: Get A List of Current Sessions

    No you cannot, I have not tried it so I cannot specify what will happen but I would definitely advise against it!
    Stressed_Simon Guest

  19. #18

    Default Re: Get A List of Current Sessions

    Ok. Does Application.cfc work like Application.cfm as that it is ran everytime
    a .cfm file is requested from the server?

    Am I able to specify variables and everything in a application.cfc file like i
    can in a application.cfm. I understand that the comands will be different
    because .cfm is a tag based file and .cfc is a script based file, but can i
    essentially do the same thing in both?

    jeby Guest

  20. #19

    Default Re: Get A List of Current Sessions

    It is not run on every page request, but the onRequestStart() and onRequest()
    methods are called in that order before the page is processed and the
    onRequestEnd() method is run after a page executes. So with a slight retink you
    can use these to emulate the Application.cfm and OnRequestEnd.cfm functionality.

    As I said before once you are familiar with the use of CFCs this is pretty
    easy to get your head around.


    Stressed_Simon Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139