Tracking loggins

Last post 01-24-2007 9:27 AM by maxl. 5 replies.

Sort Posts:

  • Tracking loggins

    01-22-2007, 4:09 PM
    • Member
      121 point Member
    • maxl
    • Member since 07-28-2006, 9:17 AM
    • Posts 37

    Hi, I'm trying to use  the following code to track wich user log in to my site and for how long.

      

        Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

    Session.Item("logged_in_date") = "01-01-1900" End Sub

    Protected Sub
    log_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles log.LoggedIn
    'user has logged in via login control Session.Item("logged_in_date") = DateTime.Now

    End Sub

    Protected Sub
    lsStatus_LoggedOut(ByVal sender As Object, ByVal e As System.EventArgs) Handles lsEtat.LoggedOut

    If Session.Item("logged_in_date") <> "01-01-1900" Then 'user is loggin out via login status Dim Q As New DataMotorisationTableAdapters.QUERY
    'inserts into my log table
    Q.INSERT_SESSIONS(Session.Item("Id_User"), Session.Item("logged_in_date"), DateTime.Now, Request.UserHostAddress)

    Q = Nothing Session.Item("logged_in_date") = "01-01-1900" End If

    End Sub

     

    This works fine but if the user just closes the browser I don't get the event.  I've tried the following code in the session end event, but the Session_End never seems to fire :

      

        Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when a session ends.
    ' Note: The Session_End event is raised only when the sessionstate mode
    ' is set to InProc in the Web.config file. If session mode is set to StateServer
    ' or SQLServer, the event is not raised.
    If Session.Item("logged_in_date") <> "01-01-1900" Then

    Dim
    Q As New DataMotorisationTableAdapters.QUERY

    Q.INSERT_SESSIONS(Session.Item("Id_User"), Session.Item("logged_in_date"), DateTime.Now, Request.UserHostAddress)

    Q = Nothing Session.Item("logged_in_date") = "01-01-1900" End If Session.Clear() End Sub
     
     
  • Re: Tracking loggins

    01-22-2007, 5:05 PM
    • Member
      82 point Member
    • adanacp
    • Member since 05-02-2006, 8:40 PM
    • Posts 18
    Session_OnEnd won't execute automatically when a user closes their browser window.  If I am not mistaken however, once the user session on the server actually times out, it will execute then.  You can probably get around this by opening another window in the OnUnload event of the page which contains an aspx page that will execute the Session.Abandon directly.
  • Re: Tracking loggins

    01-22-2007, 5:37 PM
    • Contributor
      4,146 point Contributor
    • ask_Scotty
    • Member since 01-06-2007, 10:52 AM
    • Warwick
    • Posts 707

    Hello my friend,

    If I were you, I would store 2 session variables, one containing the logged in time and the other containing the time that they last requested a page.  Put the following within the Application_BeginRequest event

    Session("Last_Requested_Something") = DateTime.Now

    Now within the Session_End event using these 2 variables, you can record the session start date and the date the user last looked at something giving you the approximate duration of their visit to the site. 

    To prove the events are firing, put this line into the Application_Start event: -

    Application("SessionCounter") = 0

    And the following into the Session_Start event: -

    Dim intSessions as Integer = Integer.Parse(Application("SessionCounter"))
    Application("SessionCounter") = (intSessions + 1)

    And add the following into the Session_End event: -

    Dim intSessions as Integer = Integer.Parse(Application("SessionCounter"))
    Application("SessionCounter") = (intSessions + 1)

    Then print Application("SessionCounter") on any of your pages.  Sessions last 20 minutes by default.  Change this in the web.config to see the effects.  The following sets the timeout to 1 minute: -

    <sessionState timeout="1"/>

     

    Kind regards

    Scotty

     

     

  • Re: Tracking loggins

    01-23-2007, 1:10 PM
    • Member
      121 point Member
    • maxl
    • Member since 07-28-2006, 9:17 AM
    • Posts 37

    Thanks both for your answers. 

    Well after some testing, it turns out that the Session_End event never fires even after the timeout has expired.  Is this the right event to use or is there something like a Session_TimedOut event ??

     Thanks again, Max

  • Re: Tracking loggins

    01-23-2007, 5:07 PM
    • Contributor
      4,146 point Contributor
    • ask_Scotty
    • Member since 01-06-2007, 10:52 AM
    • Warwick
    • Posts 707

    Hello my friend,

    You have the correct event.  Add the following to your web.config within the system.web section and give it another try: -

    <

    sessionState timeout="1" mode="InProc"/>

     Put a breakpoint on the Session_End event.  I am sure it will fire. 

    Kind regards

    Scotty

     

  • Re: Tracking loggins

    01-24-2007, 9:27 AM
    Answer
    • Member
      121 point Member
    • maxl
    • Member since 07-28-2006, 9:17 AM
    • Posts 37

    Thanks Scotty,

    It turns out it fires indeed, but it raised the error : "Request is not available in this context"    because of the Request.UserHostAdress part.  So I created another session variable to store the IP as the user logs in and then use it to insert in the database.

    Thanks again.

Page 1 of 1 (6 items)