locked
Store visitor details in database RRS feed

  • Question

  • User810354248 posted

    In my asp.net+VB+SQL web i am using this code to store users visit details

    Table

    CREATE PROCEDURE StoreIPAddressDetails
        @IPAddress VARCHAR(50)
    AS
    BEGIN
        INSERT INTO Visitors
                   (IpAddress,Visitor,VisitedDate)
            VALUES(@IPAddress,@Visitor,GETDATE())
    END
    GO

    Stored procedure

    USE [BaijuEP]
    GO
    /****** Object:  StoredProcedure [dbo].[StoreIPAddressDetails]    Script Date: 11/01/2017 10:35:04 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[StoreIPAddressDetails]
        @IPAddress VARCHAR(50), @Visitor VARCHAR(50)
    AS
    BEGIN
        INSERT INTO Visitors (IpAddress, Visitor, VisitedDate)
        VALUES(@IPAddress,@Visitor,GETDATE())
    END
    

    Code in webpage

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
           If Not Me.IsPostBack Then
                Me.StoreVisitorIPDetails()
            End If
    End sub
    
     Private Sub StoreVisitorIPDetails()
            Dim ipAddress As String
            ' Gets Client IP Address
            ipAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
            If ipAddress = "" OrElse ipAddress Is Nothing Then
                ipAddress = Request.ServerVariables("REMOTE_ADDR")
            End If
            Dim Visitor As String
            Visitor = Session("Sect")
    
            Dim strConnString As String = ConfigurationManager.ConnectionStrings("HonyLt_BVeh_BaijuEP").ConnectionString
            Dim con As New SqlConnection(strConnString)
    
            Dim cmd As New SqlCommand("StoreIPAddressDetails")
            cmd.CommandType = CommandType.StoredProcedure
    
            cmd.Parameters.AddWithValue("@IPAddress", ipAddress)
            cmd.Parameters.AddWithValue("@Visitor", Visitor)
            cmd.Connection = con
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            con.Dispose()
        End Sub

    This code stores Username and Time of visit and IP Address in  a table

    I want to add exit time also 

    as when the user entered and when exited from web page

    Wednesday, November 1, 2017 5:07 AM

Answers

  • User-1838255255 posted

    Hi Baiju EP,

    According to your description, I am not clear what event time you want to log, login and logout time or load page and close page time?  

    If it is the situation one, I think you could use this code to get the current time in your login and logout event.

    DateTime dt = DateTime.Now;
    

    If it is the situation two, I think you could try the following code to log the time of page open:

    DateTime dt;
            protected void Page_Load(object sender, EventArgs e)
            {
                dt = DateTime.Now;
            }
            // insert dt in the insert event 

    About how to capture the page close event and log the current time, then use AJAX pass time to code behind, please check this link:

    https://stackoverflow.com/questions/1631959/how-to-capture-the-browser-window-close-event 

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 2, 2017 7:33 AM