|
||
| Inside Technique : Creating Counters with ASP : Advanced Browser-Based Counters Now, let's revisit our original goal and create a browser counter. To determine what browser the visitor is using, we analyze the user-agent string provided as part of the http header. This string contains information identifying the browser, platform, and version. Below we process the user-agent string to determine what browser is being used:
sub application_onstart
' Initialize
Dim objFSO, sPath,sToken,sValue
Set objFSO = CreateObject("Scripting.FileSystemObject")
sPath = Server.mapPath("counters.inc")
if objFSO.fileExists(sPath) then
Set objFile = objFSO.OpenTextFile(sPath)
while not (objFile.AtEndOfStream)
sToken = objFile.readLine
sValue = objFile.readLine
Application(sToken) = sValue
wend
end if
objFile.close()
end sub
function SaveCounters()
Dim objFSO, sPath, c
Set objFSO = CreateObject("Scripting.FileSystemObject")
sPath = Server.mapPath("counters.inc")
Set objFile = objFSO.CreateTextFile(sPath)
for each c in application.contents
if left(c,3)="NUM" then
objFile.write c & chr(13) & chr(10)
objFile.write application(c) & chr(13) & chr(10)
end if
next
objFile.close()
end function
sub session_onstart
' Session Starts
sAgent = Request.ServerVariables("HTTP_USER_AGENT")
application.lock
if (inStr(sAgent,"MSIE")>0) then
application("num_ie") = application("num_ie") + 1
elseif inStr(sAgent,"Mozilla")>0 then
application("num_nav") = application("num_nav") + 1
else
application("num_other") = application("num_ie") + 1
end if
application("num_visitors") = application("num_visitors") + 1
if application("num_visitors") mod 50=0 then
SaveCounters()
end if
application.unlock
End Sub
Sub Application_OnEnd
SaveCounters
End Sub
The above script is a complete framework for tracking and persisting counters. Additional counters are defined simply by prefixing the application property with "num". All application properties with this prefix are automatically persisted into the counters.inc file. Outputting any of these counters onto your web-page is extremely simple. You merely response.write the corresponding application property. For example, to output all counters:
<%
Dim c
for each c in application.contents
if left(c,3)="NUM" then
response.write(c & ":" & "<BR>")
response.write(application.contents(c) & "<BR>")
end if
next
%>
Or in a more likely scenario, you will output individual counters specifically:
Total Visitors: <%=Application("num_visitors")%>
You can easily create more counters by further analyzing the user-agent string. For example. you can test for Mac or Windows with the following statements:
if (instr(sAgent,"Win")) > 0
application("num_win") = application("num_win") + 1
elseif (instr(sAgent,"Mac")) > 0
application("num_mac") = application("num_mac") + 1
end if
This concludes our introduction on creating counters usign ASP. ASP's session and application objects greatly simplify adding real-time counters to your site. By combining this with the user-agent string, you can maintain and track interesting statistics on your users. Page 1:Creating Counters with ASP © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |