In your global.asa, Session_OnStart, add:
'Update user database
set conntemp=server.createobject("adodb.connection")
cnpath="DBQ=" & server.mappath
("/stevesmith/data/timesheet.mdb")
conntemp.Open "DRIVER={Microsoft Access Driver (*.mdb)}; "
& cnpath
sqlString = "INSERT INTO user_log " & _
"
(user_id,log_date,action_code,browser,ip_address) " & _
" VALUES ('Unknown',now,'L','" & _
Request.ServerVariables("HTTP_USER_AGENT") & "','"
& _
Request.ServerVariables("REMOTE_HOST") & "')"
Set logRS=conntemp.execute(sqlString)
Set logRS=nothing
Include on any page whose hits you wish to track:
'Update pagelog
Dim hdrconntemp
Dim hdrcnpath
Dim hdrsqlString
Dim hdrlogRS
set hdrconntemp=server.createobject("adodb.connection")
hdrcnpath="DBQ=" & server.mappath
("/stevesmith/data/timesheet.mdb")
hdrconntemp.Open "DRIVER={Microsoft Access Driver
(*.mdb)}; " & hdrcnpath
hdrsqlString = "INSERT INTO page_log " & _
" (page,log_date,action_code,browser,ip_address) "
& _
" VALUES ('" & Request.ServerVariables
("SCRIPT_NAME") & "',now,'L','" & _
Request.ServerVariables("HTTP_USER_AGENT") & "','"
& _
Request.ServerVariables("REMOTE_HOST") & "')"
Set hdrlogRS= hdrconntemp.execute(hdrsqlString)
Set hdrlogRS=nothing
Naturally, you'll need to create the tables and the DSN yourself for
your own site. The table setup should be fairly obvious from the
INSERT statements. I called the tables user_log and page_log. Lastly,
you will need to display your statistics somewhere. Here is the
source code for usage.asp:
1
2 <!--#include virtual="stevesmith/top.asp"-->
3 4 ' Initialize Variables
5 Dim MyServer 'Address Server Portion
6 Dim MyPath 'Address Path Portion
7 Dim MySelf 'Full HTTP Address
8 Dim starttime 'Time page began
9 Dim objConnect 'Default Connection Object
10 Dim cnpath 'Connection path
11 Dim objCmd 'Default Command Object
12 Dim objRst 'Default Recordset Object
13 Dim total 'Total logins
14 Dim totalhits 'Total page hits
15 Dim successfullogins 'Number of Successful logins
16 Dim failedlogins 'Number of failed logins
17 Dim IE 'Number of Internet Explorer Hits
18 Dim Net 'Number of Netscape Navigator Hits
19 Dim I 'Loop control variable
20 Dim hourhits 'Hits in last hour
21 Dim hourusers 'Users in last hour
22 Dim dayhits 'Hits in last day
23 Dim dayusers 'Users in last day
24 Dim weekhits 'Hits in last week
25 Dim weekusers 'Users in last week
26 Dim monthhits 'Hits in last month
27 Dim monthusers 'Users in last month
28 Dim tmpDate,tmpDate2 'Date
29
30 MyServer=Request.ServerVariables("SERVER_NAME")
31 MyPath=Request.ServerVariables("SCRIPT_NAME")
32 MySelf="HTTP://" & MyServer & MyPath
33 %>
34
35
36 Usage
37
38
39 /stevesmith/images/whtmarb.jpg">
40 >/stevesmith/index.asp">
41
>/stevesmith/images/return.gif"
42 ALIGN="LEFT" ALT="Return to Stevenator's ASP Page" WIDTH="40"
HEIGHT="40">
43
44
Usage (May 1998 to February 1999)
45 46 starttime = now
47
48 'Establish connection
49 Set objConnect = Server.CreateObject("ADODB.Connection")
50 cnpath="DBQ=" & server.mappath("/stevesmith/data/timesheet.mdb")
51 objConnect.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " &
cnpath
52
53 'Create Command object
54 Set objCmd = Server.CreateObject("ADODB.Command")
55 Set objCmd.ActiveConnection=objConnect
56 objCmd.CommandType = adCmdText
57
58 'Set up objRst
59 Set objRst = Server.CreateObject("ADODB.Recordset")
60 Set objRst.ActiveConnection=objConnect
61 Set objRst.Source = objCmd
62
63 'Get users/hits in last hour
64 tmpDate = DateAdd("h",-1,now())
65 objCmd.CommandText = "SELECT count(user_id) as cnt FROM
user_log " & _
66 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
67 objRst.Open
68 hourusers = objRst("cnt")
69 objRst.Close
70 objCmd.CommandText = "SELECT count(ip_address) as cnt FROM
page_log " & _
71 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
72 objRst.Open
73 hourhits = objRst("cnt")
74 objRst.Close
75
76 'Get users/hits in last day
77 tmpDate = DateAdd("d",-1,now())
78 objCmd.CommandText = "SELECT count(user_id) as cnt FROM
user_log " & _
79 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
80 objRst.Open
81 dayusers = objRst("cnt")
82 objRst.Close
83 objCmd.CommandText = "SELECT count(ip_address) as cnt FROM
page_log " & _
84 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
85 objRst.Open
86 dayhits = objRst("cnt")
87 objRst.Close
88
89 'Get users/hits in last week
90 tmpDate = DateAdd("d",-7,now())
91 objCmd.CommandText = "SELECT count(user_id) as cnt FROM
user_log " & _
92 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
93 objRst.Open
94 weekusers = objRst("cnt")
95 objRst.Close
96 objCmd.CommandText = "SELECT count(ip_address) as cnt FROM
page_log " & _
97 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
98 objRst.Open
99 weekhits = objRst("cnt")
100 objRst.Close
101
102 'Get users/hits in last month
103 tmpDate = DateAdd("m",-1,now())
104 objCmd.CommandText = "SELECT count(user_id) as cnt FROM
user_log " & _
105 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
106 objRst.Open
107 monthusers = objRst("cnt")
108 objRst.Close
109 objCmd.CommandText = "SELECT count(ip_address) as cnt FROM
page_log " & _
110 " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
111 objRst.Open
112 monthhits = objRst("cnt")
113 objRst.Close
114
115 objCmd.CommandText = "SELECT count(user_id) as cnt FROM
user_log " & _
116 " WHERE action_code = 'L'"
117 objRst.Open
118 total = objRst("cnt")
119 objRst.Close
120 objCmd.CommandText = "SELECT count(*) as cnt" & _
121 " FROM page_log "
122 objRst.Open
123 totalhits = objRst("cnt")
124 %>
125
126 Your IP: 127 Item | Last Hour | Last Day | Last Week | Last Month | Total |
Logins: | | | | | |
Page Hits: | | | | | |
154
155 <!-- Display Total Hits by UserID, with IP Address --> 156 157 objRst.Close 158 objCmd.CommandText = "SELECT count(user_id) as cnt, user_id, ip_address, max(log_date) as ld" & _ 159 " FROM user_log WHERE " & _ 160 " action_code = 'L' " & _ 161 " GROUP BY ip_address,user_id " & _ 162 " ORDER BY ip_address " 163 objRst.Open 164 %> 165 Hits by User Name | IP Address | # of Logins | Last Login |
Chopped for Brevity. Source code available 175 HREF="http://www.aspalliance.com/stevesmith/samples/sitestats.asp">her e. |
| =objRst("ip_address")%> | ( %) | > |
179 <!-- Display Total Hits by Page, with IP Address --> 180 181 objRst.Close 182 objCmd.CommandText = "SELECT count(page) as cnt, page, max (log_date) as ld" & _ 183 " FROM page_log WHERE " & _ 184 " action_code = 'L' " & _ 185 " GROUP BY page " & _ 186 " ORDER BY count(page) DESC " 187 objRst.Open 188 %> 189 Hits by Page Page | # of Hits | Last Hit |
201 202 | 204 205 ( 206 207 %) | 209 210 |
218 219
220 <!-- Display total hits by Browser type --> 221 222 objRst.Close 223 objCmd.CommandText = "SELECT browser, count(*) as cnt " & _ 224 " FROM user_log " & _ 225 " WHERE browser is not null " & _ 226 " GROUP BY browser " & _ 227 " ORDER BY count(browser) desc" 228 objRst.Open 229 %> 230 Hits by 232 Browser Browser | Hits |
249 250 | 252 253 ( 254 255 %) 256 |
264
265 <!-- Display percent Explorer vs Netscape users --> 266 Hits by 268 Browser Netscape | Explorer | Other |
277 278 ( 279 280 %) | 282 283 ( 284 285 %) | 287 288 ( 289 290 %) |
294
295 <!-- Display total hits by IP and browser - what browser does each user prefer --> 296 297 objRst.Close 298 objCmd.CommandText = "SELECT ip_address, browser, count(*) as cnt " & _ 299 " FROM user_log " & _ 300 " WHERE action_code = 'L' " & _ 301 " GROUP BY browser, ip_address " & _ 302 " ORDER BY ip_address" 303 objRst.Open 304 %> 305 Browser Hits by 307 User User | Browser | Hits |
Chopped for Brevity(10 lines shown). Source code available 315 HREF="http://www.aspalliance.com/stevesmith/samples/sitestats.asp">her e. |
324 | 326 327 | 329 330 ( 331 332 %) 333 |
341
342 <!-- Display last ten users and time of use --> 343 344 objRst.Close 345 objCmd.CommandText = "SELECT ip_address, " & _ 346 " log_date FROM user_log WHERE " & _ 347 " action_code = 'L' " & _ 348 " ORDER BY log_date DESC" 349 objRst.Open 350 %> 351 Last 10 353 Users | Date | IP Address |
366 367 | 368 369 | 371 372 |
378 Total load time: 379 380 381 seconds 382 383 <!--#INCLUDE VIRTUAL="/stevesmith/bottom.asp"-->
384
385
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-124701/,如需转载,请注明出处,否则将追究法律责任。