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 <% OPTION EXPLICIT %>
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: <%=Session("IP")%>
127
128
129 Item |
130 Last Hour |
131 Last Day |
132 Last Week |
133 Last Month |
134 Total |
135
136
137 Logins: |
138 <%=hourusers%> |
139 <%=dayusers%> |
140 <%=weekusers%> |
141 <%=monthusers%> |
142 <%=total%> |
143
144
145 Page Hits: |
146 <%=hourhits%> |
147 <%=dayhits%> |
148 <%=weekhits%> |
149 <%=monthhits%> |
150 <%=totalhits%> |
151
152
153
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
166 Hits by User
167
168 Name |
169 IP Address |
170 # of Logins |
171 Last Login |
172
173
174 Chopped for Brevity. Source code available 175 HREF="http://www.aspalliance.com/stevesmith/samples/sitestats.asp">her e. |
176
177 <!--<%' Do While Not objRst.EOF%> | align="CENTER"> <%=objRst("user_id")%> <% =objRst("ip_address")%> | <%=objRst("cnt")%> ( <%=fix(CInt(objRst("cnt"))*100/total)%> %) | <%=objRst("ld")% > |
<%' objRst.MoveNext' Loop%>-->
178
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
190 Hits by Page
191
192 Page |
193 # of Hits |
194 Last Hit |
195
196 <%
197 Do While Not objRst.EOF
198 %>
199
200 201 <%=objRst("page")%> 202 |
203 204 <%=objRst("cnt")%> 205 ( 206 <%=fix(CInt(objRst("cnt"))*100/totalhits)%> 207 %) |
208 209 <%=objRst("ld")%> 210 |
211
212 <%
213 objRst.MoveNext
214 Loop
215 %>
216
217
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
231 Hits by
232 Browser
233
234 Browser |
235 Hits |
236
237 <%
238 IE = 0
239 Net = 0
240 Do While Not objRst.EOF
241 If InStr(objRst("browser"),"MSIE") Then
242 IE = IE + CInt(objRst("cnt"))
243 ElseIf InStr(objRst("browser"),"Mozilla") Then
244 Net = Net + CInt(objRst("cnt"))
245 End If
246 %>
247
248 249 <%=objRst("browser")%> 250 |
251 252 <%=objRst("cnt")%> 253 ( 254 <%=fix(CInt(objRst("cnt"))*100/total)%> 255 %) 256 |
257
258 <%
259 objRst.MoveNext
260 Loop
261 %>
262
263
264
265 <!-- Display percent Explorer vs Netscape users -->
266
267 Hits by
268 Browser
269
270 Netscape |
271 Explorer |
272 Other |
273
274
275
276 277 <%=Net%> 278 ( 279 <%=fix(Net*100/total)%> 280 %) |
281 282 <%=IE%> 283 ( 284 <%=fix(IE*100/total)%> 285 %) |
286 287 <%=total-Net-IE%> 288 ( 289 <%=fix((total-Net-IE)*100/total)%> 290 %) |
291
292
293
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
306 Browser Hits by
307 User
308
309 User |
310 Browser |
311 Hits |
312
313
314 Chopped for Brevity(10 lines shown). Source code available 315 HREF="http://www.aspalliance.com/stevesmith/samples/sitestats.asp">her e. |
316
317 <%
318 I = 0
319 Do While I < 10
320 I = I + 1
321 %>
322
323 <%=objRst("ip_address")%> 324 |
325 326 <%=objRst("browser")%> 327 |
328 329 <%=objRst("cnt")%> 330 ( 331 <%=fix(CInt(objRst("cnt"))*100/total)%> 332 %) 333 |
334
335 <%
336 objRst.MoveNext
337 Loop
338 %>
339
340
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
352 Last 10
353 Users
354
355 |
356 Date |
357 IP Address |
358
359 <%
360 I = 0
361 Do While (Not objRst.EOF) AND (I < 10)
362 I = I + 1
363 %>
364
365 366 <%=I%> 367 | 368 <%=objRst("log_date")%> 369 |
370 371 <%=objRst("ip_address")%> 372 |
373
374 <% objRst.MoveNext
375 Loop %>
376 <!-- Display Total Server time to generate this data -->
377
378 Total load time:
379
380 <%=datediff("s",starttime,now)%>
381 seconds
382 383 <!--#INCLUDE VIRTUAL="/stevesmith/bottom.asp"-->
384
385
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10294527/viewspace-124701/
,如需转载,请注明出处,否则将追究法律责任。