Subject: Global.asa and user tracking
Posted By: DynaCube Post Date: 1/12/2007 9:28:35 PM
Hey Guys. Can someone help me out here? I'm working on my new site and I'm currently trying to integrate user tracking, but it's not working. I'm using global.asa to insert data into my Access database with the following code:


Code:
<OBJECT RUNAT=Server SCOPE=Session ID=BC PROGID="MSWC.BrowserType"></OBJECT>


<script language="vbscript" runat=server>

Sub Session_OnStart

    ' ADO Constants
    Const adOpenForwardOnly = 0
    Const adOpenKeyset = 1
    Const adOpenDynamic = 2
    Const adOpenStatic = 3

    ' LockTypeEnum Const Values
    Const adLockReadOnly = 1
    Const adLockPessimistic = 2
    Const adLockOptimistic = 3
    Const adLockBatchOptimistic = 4         

'Define Data to Insert into Log

    'Sniff Browser Info From Header.
    Browser = Trim(Request.ServerVariables("HTTP_USER_AGENT"))

    'Get Users Browser Language Setting
    Language = _
        Trim(Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"))

    ' Where did they come from? Oh! That's Where!
    Referer = Trim(Request.ServerVariables("HTTP_REFERER"))
    SessionID = Trim(Session.SessionID)

    'Get User IP
    UserIP = Trim(Request.ServerVariables("REMOTE_HOST"))
    Session("IP") = UserIP

'What Part of the Site did they Hit First?
    FirstPage = Trim(Request.ServerVariables("Script_Name"))

'Resolve OS and Browser details if possible
    OS = bc.platform
    UA = bc.browser
    UAversion = bc.version
    Today = Now()

    'Log week and year number stats for later retrieval
    TWeekNumber = DatePart("ww", Now())
    TYearNumber = DatePart("yyyy", Now())

' Handle exceptions
    If Language = "" Then
        Language = "Unknown"
    End If

    If Referer = "" Then
        Referer = "None"
    End If

    'Could be useful to persist REFERER info.
    Session("Referer") = Referer

    BrowserCode = "0"
    Set oSRS = Server.CreateObject("ADODB.RecordSet")
    
    'DB Connection Settings
    strSConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath ("./db/DC1.mdb") & ";"
    strSQL = "SELECT * FROM tblSession"
    oSRS.CursorType = adOpenKeyset
    oSRS.LockType = adLockOptimistic

On Error Resume Next
    oSRS.Open strSQL, strSConn

    ' Update the DB with the Info.
    oSRS.AddNew
        oSRS("Browser") = Browser
        oSRS("Language") = Language
        oSRS("Referer") =  Referer
        oSRS("SessionID") = Session.SessionID
        oSRS("UserIP") = UserIP
        oSRS("Date") = Date()
        oSRS("Time") = Time()
        oSRS("HitPage") = FirstPage
        oSRS("TWeekNumber") = TWeekNumber
        oSRS("TYearNumber") = TYearNumber
        oSRS("BrowserCode") = BrowserCode
        oSRS("OS") = OS
        oSRS("UA") = UA
        oSRS("UAversion") = UAversion
    oSRS.Update

    oSRS.Close
    Set oSRS = Nothing


    IF Err > 0 Then
        Response.Write Err.Number & " " & Err.Description
    End IF
 
End Sub

</script>

The sitelog.asp page follows:


Code:
<% Response.Buffer = False
    
    'Max Records to display per page
    iPageSize = 25
    'Double Quote
    Q = Chr(34)
    'Line Feed
    CR = vbCrLf
    
    'Check for first page
    If Request.QueryString("page") = "" Then
        iPageCurrent = 1
    Else
        'Not 1st page, grab page # From querystring
        iPageCurrent = CInt(Request.QueryString("page"))
    End If
    
    'Do we want to show Browser details? If so Click the checkbox.
    IF lCase(Request("ShowBrowserDetails")) = "on" Then
        strChecked = "Checked"
        ShowBrowserDetails = True
    Else
        strChecked = ""
        ShowBrowserDetails = False
    End IF
%>

<html>
<head>
<title>Dyna-Cube | Site Log</title>
<META Name="ROBOTS" Content="NOINDEX, NoFollow"
    
</head>

<body>
<%  
    Set RS = Server.CreateObject("ADODB.RecordSet")
    Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath ("./db/DC1.mdb") & ";"
    strSQL = "SELECT * FROM tblSession"

    on Error Resume Next
    RS.PageSize = iPageSize
    RS.CacheSize = iPageSize
    RS.CursorLocation = adUSeClient
    RS.Open strSQL, Conn, 3, 3    
    
    'Total # of pages
    iPageCount = RS.PageCount
    
    
IF iPageCount = 0 Then
    Response.Write "No records found!"
Else
    ' Move to the selected page
    RS.AbsolutePage = iPageCurrent

    Response.Write "Page <B>" & iPageCurrent & "</B> of " _
        & "&nbsp;<B>" & iPageCount & "</B>&nbsp;&nbsp;&nbsp;" _
        & RS.RecordCount & " Total Records." & vbCrLf

    
%>
<Form Method="POST" Name="frmShowMoreDetails">
<font face="arial" size=1>Show Browser Details?</font>
    <input type="checkBox" name="ShowBrowserDetails"
    <%= strChecked %>
    onClick="document.frmShowMoreDetails.submit()">
</form>

<table border=0 width="100%" cellspacing="4" cellpadding="4">
<tr bgColor="skyBlue">

<td valign="top">Referer</td>
<td valign="top" width="20%"><b>IP Address</b></td>
<td valign="top"><b>Date</b></td>
<td valign="top"><b>Time</b></td>
<td valign="top"><b>First Page</b></td>
<% IF ShowBrowserDetails = True Then %>
    <td valign="top"><b>Browser</b></td>
    <td valign="top"><b>Version</b></td>
<% End IF %>

</tr>

<%  
    'counter to keep track of record #'s displayed
    iRecordsShown = 0
    'Loop thru records until Page Size is reached
    Do While iRecordsShown < iPageSize AND Not RS.EOF
    IF RS("Referer") = "None" Then
        strRef = "<b>Bookmark</b>"
    Else
        strRef = "<a href=" & Q & "#" & Q & " onClick=" & Q _
            & "var j = window.open('" & RS("Referer") _
            & "', 'newWin'); j.focus();" & Q & ">" _
            & urlDecode(RS("Referer")) & "</a>"
    End IF
%>

<tr bgColor="silver">
    <td valign="top" width="20%" WRAP=HARD><%= strRef %></td>
    <td valign="top"><b> <%= RS("UserIP") %></b></td>
    <td valign="top"><b> <%= RS("Date") %></b></td>
    <td valign="top"><b> <%= RS("Time") %></b></td>
    <td valign="top"><b> <%= RS("HitPage") %></b></td>
<% IF ShowBrowserDetails = True Then %>
    <td valign="top"><b> <%= RS("Browser") %></b></td>
    <td valign="top"><b> <%= RS("UAversion") %></b></td>
<% End IF %>
</tr>

<%  
    RS.MoveNext
        'increment counter
        iRecordsShown = iRecordsShown + 1
    Loop
    
End IF
    
    RS.Close
    Set RS = Nothing
    
    Response.Write "</table>"
    
    IF iPageCurrent > 1 Then
       Response.Write "<input type=button " _
          & "name=" & Q & "btnPrev" & Q & " " _
          & "value=" & Q & "<< Previous "  & Q & " " _
          & "onClick=" & Q & "window.location='SiteLog.asp?page=" _
          & iPageCurrent - 1 & "'" & Q & ">" & vbCrLf
    End IF
    
    If iPageCurrent < iPageCount  Then
       Response.Write "<input type=button " _
          & "name=" & Q & "btnNext" & Q & " " _
          & "value=" & Q & "Next >>" & Q & " " _
          & "onClick=" & Q & "window.location='SiteLog.asp?page=" _
          & iPageCurrent + 1 & "'" & Q & ">" & vbCrLf
    End IF

' Function to help make the referer data a little more readable.
' Replaces urlEncoded data with it's original character
Function urlDecode(str)

    str = Replace(str, "%3F", "?")
    str = Replace(str, "%2F", "/")
    str = Replace(str, "%7C", "|")
    str = Replace(str, "%5C", "\")
    str = Replace(str, "%21", "!")
    str = Replace(str, "%40", "@")
    str = Replace(str, "%23", "#")
    str = Replace(str, "%24", "$")
    str = Replace(str, "%25", "%")
    str = Replace(str, "%5E", "^")
    str = Replace(str, "%26", "&")
    str = Replace(str, "%2A", "*")
    str = Replace(str, "%28", "(")
    str = Replace(str, "%29", ")")
    str = Replace(str, "%7B", "{")
    str = Replace(str, "%7D", "}")
    str = Replace(str, "%3A", ":")
    str = Replace(str, "%2E", ".")
    str = Replace(str, "%2D", "-")
    str = Replace(str, "%5B", "[")
    str = Replace(str, "%5D", "]")
    str = Replace(str, "%2C", ",")
    str = Replace(str, "%3D", "=")
    str = Replace(str, "%2B", "+")
    str = Replace(str, "%2D", "-")
    str = Replace(str, "%5F", "_")
    str = Replace(str, "%7E", "~")
    str = Replace(str, "%60", "`")
    str = Replace(str, "%27", "'")
    str = Replace(str, "%22", Chr(34))

    urlDecode = str
End Function
    
%>

I don't get any errors when I visit the site I'm working on but it doesn't insert a record either. I know the connection string is correct because it works fine for my login. Anyone have any ideas what I'm doing wrong?

Thanks to any and everyone who can help at all.
Reply By: DynaCube Reply Date: 1/12/2007 10:24:21 PM
I got it fixed guys.  I just had to set my permissions to write on the directory that held my DB.

Thanks.

Go to topic 54797

Return to index page 65
Return to index page 64
Return to index page 63
Return to index page 62
Return to index page 61
Return to index page 60
Return to index page 59
Return to index page 58
Return to index page 57
Return to index page 56