Program for Web counter

Source: Internet
Author: User
Tags count readline
Program | counter | Web page Counter DIY
As the network is on its way, web counters are also popular. In fact, most Web sites have web counters that reflect the amount of traffic on the site. Counter source is very wide, FrontPage and other Web pages editor with a Web page counter, and some sites also provide free counter download. In fact, familiar with the ASP programming, it is easy to make a counter yourself. An implementation method is described below.
The counter principle is: When the first use of the Web page, the initial value of 1, the next time each request page, the counter value plus 1. So we just put a text file on the server counter.txt, the contents of the text file has the value of the counter, each time after each request page, read the number of text file counters, plus 1 display, and then change the original value to add 1 after the value, save to the text file. As for the initial set 1, on the server can not build Counter.txt, in the Web page, first judge whether there are counter.txt files on the server, did not generate Counter.txt, write 1 in the Counter.txt, the page display counter value 1, complete the initial 1. You can set the initial value each time you want to delete the Counter.txt file in the specified directory.
The concrete operation must have the display number 0, 1, the 2....9 image file, 0.gif, 1.gif, 2.gif ... 9.gif, the file can not be too large, general 18*25 can be. You want to put the counter page layout design finished, and then changed to ASP file, the following code input to show the counter where, when used, the program will automatically create a counter.txt file under the virtual directory count. Deletes the file when the initial value is placed. Yes, the virtual directory count must give everyone the Write permission.
<%
Const ForReading = 1, ForWriting = 2, ForAppending =3
Const Tristateusedefault =-2, TristateTrue =-1, tristatefalse = 0
Filepath=server.mappath ("/count")
filename=filepath+ "\counter.txt"
Set Fs=createobject ("Scripting.FileSystemObject")
If fs.fileexists (filename) Then



Set F=fs.getfile (filename)
Set ts = F.openastextstream (ForReading,
Tristateusedefault)
S=ts.readline+1
Ts.close
Else
Fs.createtextfile (filename)
Set F=fs.getfile (filename)
S=1
End If

' Write data to Counter.txt
Set ts = F.openastextstream (ForWriting,
Tristateusedefault)
Ts.writeline (CStr (s))
Ts.close

' Show counters
S=cstr (s+1000000)
S=mid (s,2,6)
For I=1 to 6
Response.Write "& ". gif ' width= ' height= ' >"
Next

%>
Impatient friend to ask, you this counter value shows 6-bit count, if you want to show 8-bit count, how to do? Don't worry, I'll give you a formula when I finish the next example.
The disadvantage of this counter is that every time you refresh the page counter add 1, this is because every time the page is refreshed, the system thinks you re requesting the page, and if you don't enter the site from the main page, the counter doesn't change the count. If you want to



More precisely, as long as the above code slightly modified, put in your Global.asa Session_OnStart, so that only new users to enter the site, the counter will add 1. Users who have entered the site refresh the page without causing the counter count to change, and the counters can capture you no matter which page you are from.
<script Language=vbscript runat=server>
Sub Application_OnStart
Filepath=server.mappath ("/count")
filename=filepath+ "\counter.txt"
Set Fs=createobject ("Scripting.FileSystemObject")
If not fs.fileexists (filename) Then
Fs.createtextfile (filename)
Set F=fs.getfile (filename)
S=1
Set ts = F.openastextstream (2,-2)
Ts.writeline (CStr (s))
Ts.close
Else
Set F=fs.getfile (filename)
Set ts = f.openastextstream (1,-2)
S=ts.readline+1
Ts.close
End If
Application ("Visitor") =s
End Sub

Sub Session_OnStart
Session.timeout=5
Application ("Visitor") =application ("visitor") +1
Set F=fs.getfile (filename)
Set ts = F.openastextstream (2,-2)



Ts.writeline (CStr (Application ("Visitor"))
Ts.close
End Sub
</script>
Displays the counter's image in the corresponding section of the Web page based on the value of application ("visitor").
<%
S=CSTR (Application ("visitor") +10^6)
S=mid (s,2,6)
For I=1 to 6
Response.Write "&mid (s,i,1) & ". gif ' width= ' height= ' >"
Next
%>
To display an n-bit counter, simply change the code above to:
<%
S=CSTR (Application ("visitor") +10 ^n)
S=mid (S,2,n)
For I=1 to N
Response.Write "&mid (s,i,1) & ". gif ' width= ' height= ' >"
Next
%>
This to get 8-bit counters, as long as the addition of n=8 can be.
If you feel that every time a user enters the site, counter.txt write too frequently, can be set in the Session_OnStart, in application
("visitor") writes the count value into the counter.txt when it is a multiple of 10.
What do you think? Let's do it now!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.