Go Back   Internet Business Forums > Design & Development

Reply
 
Thread Tools Display Modes
Old 23-11-2006, 09:59 PM   #1 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default [SCRIPT] ASP- Hit Counter

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 %>
army is offline   Reply With Quote
Old 23-11-2006, 10:01 PM   #2 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [SCRIPT] ASP- Hit Counter

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 %>
army is offline   Reply With Quote
Old 23-11-2006, 10:04 PM   #3 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [SCRIPT] ASP- Hit Counter

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>
army is offline   Reply With Quote
Old 23-11-2006, 10:05 PM   #4 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [SCRIPT] ASP- Hit Counter

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
%>
Place following code where you want your counter to appear.

Code:
<%
'Write the pagecount
Response.Write(strCount)
%>
You can surround the code with HTML formatting to change the appearance of the text counter. eg.

Code:
<font color="#FF0000"><strong><% Response.Write(strCount) %></strong></font>
army is offline   Reply With Quote
Old 24-11-2006, 12:21 AM   #5 (permalink)
Senior Member
 
Join Date: Nov 2006
Posts: 225
Default Re: [SCRIPT] ASP- Hit Counter

thanks for sharring army!
roy77 is offline   Reply With Quote
Old 28-11-2006, 08:53 PM   #6 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [SCRIPT] ASP- Hit Counter

Quote:
Originally Posted by daniel View Post
thanks for sharing that
the bad part is my host doesnt support asp
im going to think about swithcing hosts
Lol. ASP isn't that important . If your host has PHP, it is also good .
army is offline   Reply With Quote
Old 29-11-2006, 07:55 PM   #7 (permalink)
Senior Member
 
syntex612's Avatar
 
Join Date: Aug 2006
Location: Paksitan
Posts: 148
Send a message via ICQ to syntex612 Send a message via AIM to syntex612 Send a message via MSN to syntex612 Send a message via Yahoo to syntex612
Default Re: [SCRIPT] ASP- Hit Counter

great tutorial thanks alot
syntex612 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


All times are GMT +1. The time now is 08:27 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.