|
|
#1 (permalink) |
|
co-admin
Join Date: Oct 2006
Location: Belgium
Posts: 666
|
The code bellow is prepared to be added to your asp pages and it will automatically start counting hits for the page containning it. You may copy the same code to several ASP pages, and the script will count hits for all pages individually.
You must create a subfolder named "Mycounters" within the cgi-bin directory. The name of the subdirectory may be changed in the script. The script will atomatically create one counter (one file within subfolder "Mycounters") per each page containing the script bellow in the first visit. Each time a page is visited the corresponding counter will be open, read the number of hits, increased by one and the new number of hits save to the same counter file. The name of individual counters within subfolder "Mycounters" will shown the path to the page visited. For example: Hits at [b]/index.asp[b] will be save to /cgi-bin/mycounters/indexasp.txt Hits at [b]/mydir1/page1.asp[b] will be save to /cgi-bin/mycounters/mydir1page1asp.txt To get the number of hits in all your pages you may used the script shown in the bottom table. counter.asp Code:
<%
file=request.servervariables("PATH_INFO")
file = replace(file,"/","")
file = replace(file,".","")
if file="" then
file="otros"
end if
Set fs = CreateObject("Scripting.FileSystemObject")
Wfile=server.mappath("\") & "\cgi-bin\Mycounters\" & file & ".txt"
on error resume next
Set a = fs.OpenTextFile(Wfile)
hits = Clng(a.ReadLine)
hits = hits + 1
a.close
if error then
hits = 1
end if
Set a = fs.CreateTextFile(Wfile,True)
a.WriteLine(hits)
a.Close
%>
Number of hits: <% =hits %>
The number of hits in the corresponding counter will be increased each time your page is visited. Last line in this script will allow you to show the number of hits in your response page. In order to get the number of hits to all pages we may used the script bellow: Showallcounters.asp Code:
<%
FolderToCheck=server.mappath("\") & "\cgi-bin\Mycounters\"
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(FolderToCheck)
Set fc = f.Files
For Each f1 in fc
Wfile=f1.name
if right(Wfile,4)=".txt" then
FiletoCheck=FolderToCheck & "\" & Wfile
Set a=fs.OpenTextFile(FiletoCheck)
hits=Clng(a.ReadLine)
Response.write(Wfile & ": " & hits & "<BR>")
allhits=allhits + hits
end if
Next
%>
<HR>
All hits: <% =allhits %>
<BR>
Date: <% =Date %>
|
|
|
|
|
|
#2 (permalink) |
|
co-admin
Join Date: Oct 2006
Location: Belgium
Posts: 666
|
And an other way
This script will automatically create a file named "counter271877.txt" within "cgi-bin"directory in your site (you may change the name of the file or the subdirectory within the script). This file will be used to save the number of times the page has been requested. counter271877.asp Code:
<%
Set fs = CreateObject("Scripting.FileSystemObject")
Wfile=server.mappath("\") & "\cgi-bin\counter271877.txt"
on error resume next
Set a = fs.OpenTextFile(Wfile)
hits = Clng(a.ReadLine)
hits = hits + 1
a.close
if error then
hits = 1
end if
Set a = fs.CreateTextFile(Wfile,True)
a.WriteLine(hits)
a.Close
%>
Number of hits: <% =hits %>
|
|
|
|
|
|
#3 (permalink) |
|
co-admin
Join Date: Oct 2006
Location: Belgium
Posts: 666
|
And an other way
There are many ways to track usage of a website. The simplest way is with a hit counter. The code provided below is one of the simplest hit counters that can be made in .asp. A text document (asp_count.txt) is placed in the same directory (providing that directory has write access, you may need to place the .txt document in your cgi-bin depending on your provider) as the page we want the counter to appear on. The .txt file is first read to gain the value of the last hit count, one is added to that number, written to the .txt document, closed and the counter value is written as text to the page. Refresh the content of the output page for a more complete illustration. Code:
<html>
<title>CodeAve.com(ASP Hit Counter)</title>
<body bgcolor="#FFFFFF">
<%
on error resume next
' Create a server object
set fso = createobject("scripting.filesystemobject")
' Target the text file to be opened
set act = fso.opentextfile(server.mappath("asp_count.txt"))
' Read the value of the text document
' If the text document does not exist then the on error resume next
' will drop down to the next line
counter = clng(act.readline)
' Add one to the counter
counter = counter + 1
' Close the object
act.close
' Create a new text file on the server
Set act = fso.CreateTextFile(server.mappath("asp_count.txt"), true)
' Write the current counter value to the text document
act.WriteLine(counter)
' Close the object
act.Close
' Write the counter to the browser as text
Response.Write counter
%>
</body>
</html>
|
|
|
|
|
|
#4 (permalink) |
|
co-admin
Join Date: Oct 2006
Location: Belgium
Posts: 666
|
And an other way
This simple page counter uses the File System Object to store the page count in a text file located on the server. You must have the use of the File System Object and enable the server to read and write to the text file. Code:
<%
'Using the File System Object
'Create the FSO
Set bwoFSO = Server.CreateObject("Scripting.FileSystemObject")
'Location of text file to store page count.
Set bwoFile = bwoFSO.OpenTextFile("C:\wwwroot\count.txt")
'now we read the number in the file
oldNum = CLng(bwoFile.ReadLine)
'and we close
bwoFile.Close
'we add one to the number
newNum = oldNum + 1
'we open the file to overwrite the new number to it
Set bwoFile = bwoFSO.OpenTextFile("C:\wwwroot\count.txt",2,true)
'and we overwrite it here
bwoFile.WriteLine(newNum)
'and we close the file again
bwoFile.Close
'now we use the number for our display
strCount = newNum
%>
Code:
<% 'Write the pagecount Response.Write(strCount) %> Code:
<font color="#FF0000"><strong><% Response.Write(strCount) %></strong></font> |
|
|
|