Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

track hits to web site 2

Status
Not open for further replies.

cmdematos

IS-IT--Management
Mar 7, 2001
29
US
Hello all, (from absolute newby)

I want to track hits on my web site, and I dont want to use counters to do it.

My thoughts are to track some basic things like IP address, browser type, client email address (if possible) and record the session context and URL into SQL Server.

I guess I can use the PageLoad () { if (!IsPostBack)... to do it, but where do I find this information?
 
What are you looking for?

Individual User sessions (not page counts)
Page Counts....
Hit Counts...

For just tracking how many visitor sessions you have had, you can use just the session_onstart event and increment an application variable..

If it is more detailed and page related the following code stores loads for a specific page into a sqlserver database...
This code also looks to see what (web) domain they came from...
Code:
Imports System.Text.RegularExpressions
Public Class Sql
    Inherits System.Web.UI.Page
    Dim cn As New SqlClient.SqlConnection("server=(local);database=crap;trusted_connection=yes")
    Dim sProcName As String
    Dim colParameters As New Collection

    Structure Param
        Dim name As String
        Dim type As String
        Dim size As Int16
        Dim Output As Boolean
    End Structure

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        UseAddVisitor(Request.UserHostAddress, "SQL.aspx", GetDomain(Request.UserHostAddress))
    End Sub

    Private Function GetDomain(ByVal ip As String) As String

        Dim dm As Net.Dns
        Dim he As Net.IPHostEntry
        Try
            he = dm.GetHostByAddress(ip)
            Return he.HostName()
        Catch ex As Exception
            Return ex.Message
        End Try

        dm = Nothing
    End Function

    Private Function UseAddVisitor(ByVal IP As String, ByVal Page As String, ByVal DomainFrom As String) As Boolean
        Dim con As New SqlClient.SqlConnection("server=(local);database=lynchdom;trusted_connection=yes")
        Dim cmd As New SqlClient.SqlCommand("AddVisitor", con)
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        With cmd
            .CommandType = CommandType.StoredProcedure
            With .Parameters
                .Add("@IP", SqlDbType.VarChar, 300).Value = IP
                .Add("@Page", SqlDbType.VarChar, 3000).Value = Page
                .Add("@DomainFrom", SqlDbType.VarChar, 3000).Value = DomainFrom
            End With
            con.Open()
            Try
                cmd.ExecuteNonQuery() ' For a stored proc with no records...

            Catch ex As Exception
                'msgbox ex.message
                Response.Write(ex.Message)
                Return False
                Exit Function
            Finally
                con.Close()
                cmd.Dispose()
            End Try
        End With

        Return True
    End Function

SLQ Proc

Code:
CREATE TABLE [Visitors] (
	[visitorid] [int] IDENTITY (1, 1) NOT NULL ,
	[VisitorIP] [varchar] (300) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[PageRequestTime] [datetime] NOT NULL CONSTRAINT [DF__Visitors__PageRe__3F466844] DEFAULT (getdate()),
	[PageRequested] [varchar] (3000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[DomainFrom] [varchar] (3000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	 PRIMARY KEY  CLUSTERED 
	(
		[visitorid]
	)  ON [PRIMARY] 
) ON [PRIMARY]
GO

Create  Proc AddVisitor
@IP varchar(300),
@Page varchar(3000),
@DomainFrom varchar(3000)
as
insert into Visitors (visitorIP,PageRequested,DomainFrom)
values (@Ip,@page,@domainfrom)
 
If you want to record this information for all your pages you'd be best off using an HttpModule or an event in Global.asax.

Finding IP:
Request.ServerVariables["REMOTE_ADDR"]

Browser:
Request.ServerVariables["HTTP_USER_AGENT"]

E-Mail:
You'd have to have the user give it to you (then retrieve the value, of course).

URL:
Request.ServerVariables["PATH_TRANSLATED"] +
"?" + Request.ServerVariables["QUERY_STRING"]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top