chrishirst
21-08-2004, 04:36/04:36AM
Basic ASP + MySql/Access script for recording site visitors and tracking traffic.
set up to use MySQL, but to use other SQL servers it should simply be a case of adding the correct connection string (or DSN) to the script.
The code below needs to go into an included file, change the constants to suit your server and the sub is called on the page by using <%Track()%>.
table field details included in comments.
<%
' table structure;
' fieldname - type
'==============
' id - Autonumber
' accessdate - date
' accesstime - time
' remoteip - varchar(15)
' refererurl - varchar(254)
' siteurl - varchar(254)
' method - varchar(6)
' useragent - varchar(254)
const Tracking_Table = "tablename"
const str_SQL_User = "username"
const str_SQL_Pass = "password"
const str_SQL_Server = "server"
const str_SQL_DB = "database"
dim track_on
dim str_SQL_ConnString
' str_SQL_ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database.mdb")
str_SQL_ConnString = "driver={MySQL ODBC 3.51 Driver};server=" & str_SQL_Server & ";uid=" & str_SQL_User & ";pwd=" & str_SQL_Pass & ";database=" & str_SQL_DB & ";option=16387"
track_on = true
' set true or false to turn tracking on or off
Sub Track()
if track_on = true then
dim str_REMOTE_ADDR
dim str_HTTP_REFERER
dim str_URL
dim str_REQUEST_METHOD
dim str_HTTP_USER_AGENT
dim str_Track_SQL
with request
str_REMOTE_ADDR = .servervariables("REMOTE_ADDR")
str_HTTP_REFERER = .servervariables("HTTP_REFERER")
str_URL = .servervariables("URL")
str_REQUEST_METHOD = .servervariables("REQUEST_METHOD")
str_HTTP_USER_AGENT = .servervariables("HTTP_USER_AGENT")
end with
Set conn=Server.CreateObject("ADODB.Connection")
Conn.Open str_SQL_ConnString
str_Track_SQL = "INSERT INTO " & Tracking_Table & " (accessdate,accesstime,remoteip,refererurl,siteurl,method,useragent) VALUES (now()" & ",'" & time() & "','" & str_REMOTE_ADDR & "','" & str_HTTP_REFERER & "','" & str_URL & "','" & str_REQUEST_METHOD & "','" & str_HTTP_USER_AGENT & "');"
conn.execute(str_Track_SQL)
conn.close
set conn = nothing
end if
end sub
%>
Tried to reduce width, it was driving me crazy!~ JH
set up to use MySQL, but to use other SQL servers it should simply be a case of adding the correct connection string (or DSN) to the script.
The code below needs to go into an included file, change the constants to suit your server and the sub is called on the page by using <%Track()%>.
table field details included in comments.
<%
' table structure;
' fieldname - type
'==============
' id - Autonumber
' accessdate - date
' accesstime - time
' remoteip - varchar(15)
' refererurl - varchar(254)
' siteurl - varchar(254)
' method - varchar(6)
' useragent - varchar(254)
const Tracking_Table = "tablename"
const str_SQL_User = "username"
const str_SQL_Pass = "password"
const str_SQL_Server = "server"
const str_SQL_DB = "database"
dim track_on
dim str_SQL_ConnString
' str_SQL_ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database.mdb")
str_SQL_ConnString = "driver={MySQL ODBC 3.51 Driver};server=" & str_SQL_Server & ";uid=" & str_SQL_User & ";pwd=" & str_SQL_Pass & ";database=" & str_SQL_DB & ";option=16387"
track_on = true
' set true or false to turn tracking on or off
Sub Track()
if track_on = true then
dim str_REMOTE_ADDR
dim str_HTTP_REFERER
dim str_URL
dim str_REQUEST_METHOD
dim str_HTTP_USER_AGENT
dim str_Track_SQL
with request
str_REMOTE_ADDR = .servervariables("REMOTE_ADDR")
str_HTTP_REFERER = .servervariables("HTTP_REFERER")
str_URL = .servervariables("URL")
str_REQUEST_METHOD = .servervariables("REQUEST_METHOD")
str_HTTP_USER_AGENT = .servervariables("HTTP_USER_AGENT")
end with
Set conn=Server.CreateObject("ADODB.Connection")
Conn.Open str_SQL_ConnString
str_Track_SQL = "INSERT INTO " & Tracking_Table & " (accessdate,accesstime,remoteip,refererurl,siteurl,method,useragent) VALUES (now()" & ",'" & time() & "','" & str_REMOTE_ADDR & "','" & str_HTTP_REFERER & "','" & str_URL & "','" & str_REQUEST_METHOD & "','" & str_HTTP_USER_AGENT & "');"
conn.execute(str_Track_SQL)
conn.close
set conn = nothing
end if
end sub
%>
Tried to reduce width, it was driving me crazy!~ JH