How Do You Access a Session Variable?

  1. 2 months ago

    Peter S

    Aug 11 Pre-Release Testers, Xojo Pro Huntsville, AL

    How do you reference a session variable when you're looping through the sessions? I'm trying to check a variable called Session.username to see if the user is already logged in on another session so I can kill the old one.

    I can get the session ID with:

     dim sess as WebSession
      for x = 0 to app.sessioncount - 1
        
        sess = app.SessionAtIndex(x)
        
        TextArea1.text = textarea1.text + "Session ID#" + cstr(x) + ": " + sess.Identifier
        
      next x

    But, I can't figure out how to reference the 'Session.username' variable for a specific session so I can compare it to the current one.

    Any clues?

  2. Antonio R

    Aug 11 Pre-Release Testers, Xojo Pro Europe (Italy)

    You can use session instead of webSession

    dim sess as session
    for x = 0 to app.sessioncount - 1
        
        sess = app.SessionAtIndex(x)
        
        TextArea1.text = textarea1.text + "Session ID#" + cstr(x) + ": " + sess.Identifier
        
      next x
  3. Peter S

    Aug 11 Pre-Release Testers, Xojo Pro Huntsville, AL

    Well, I saw this in the docs:

    If the user opens two browser windows/tabs to an app, the new window gets its own Session instance. Use cookies to determine if two sessions belong to the same user.

    So, to compare the username session variable from one session to another, I need to store the username in a cookie and then I can access it there?

    Trying to figure out if I'm on the right track or if there is just something obvious I'm missing like:

    thisUser = app.SessionAtIndex(x).username
  4. Peter S

    Aug 11 Pre-Release Testers, Xojo Pro Huntsville, AL

    To confirm for anyone looking for this info later, the cookies option seems to be the way to go. When the program starts up, set a cookie for the session info you want to compare. Then loop through the sessions, look at the cookies and see if a session with a different identifier has the same username.

    This is the language I used to check the cookie:

    Dim c as Integer = App.SessionCount-1
    For x = c downto 0
    sess = app.SessionAtIndex(x)
    sessionUser = sess.Cookies.value("UserLoginName")
    next x

  5. Antonio R

    Aug 11 Pre-Release Testers, Xojo Pro Europe (Italy)

    You can use a property in your session object.

  6. Peter S

    Aug 11 Pre-Release Testers, Xojo Pro Huntsville, AL

    Thanks!

or Sign Up to reply!