Website counter

xiaoxiao2021-03-06  39

As the network is large, the webpage counter is also popular. In fact, most websites have a web counter to reflect the number of visitors. The source of the counter is wide, and the web editor such as FrontPage comes with a web counter, and some sites also provide free counters download. In fact, after familiar with the ASP programming, it is easy to do a counter. Here is an implementation method.

The counter principle is: Place the initial value 1 for the first time, after each request of the web, add the counter value 1. In this way, we only need to place a text file counter.txt, the content of the text file has the value of the counter, and then the value of the counter, read the value of the counter of the text file, plus 1 display, and then change the original value to change to Add 1 value, save to text files. As for initial 1, you can not build counter.txt on the server. In the web page, first determine if there is a counter.txt file on the server, do not generate counter.txt, write 1 in counter.txt, display on the web page The counter value is 1, complete the initial set 1. Each time you remove the COUNTER.TXT file in the specified directory, you can set the initial value.

In the specific operation, there is an image file, 0.GIF, 1.gif, 2.gif ... 9.gif, 2.gif ... 9.gif, files that cannot be too large, usually 18 * 25. After designing the webline layout you want to place, change to an ASP file, enter the following code to the place where the counter is to be displayed. When you use, the program will automatically establish a Counter.txt file under the virtual directory count. Remove the file when you set the initial value. By the way, the virtual directory count must give the everyone's permissions.

<%

Const

Forreading = 1,

Forwriting = 2,

FORAPPENDING = 3

Const

Tristateusedefault = -2,

Tristatetrue = -1,

Tristatefalse = 0

FilePath = Server.mappath ("/ count")

FILENAME = filepath "/ counter.txt"

Set fs = creteObject ("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 counter

s = CSTR (S 1000000)

S = MID (S, 2, 6)

For i = 1 to 6

Response.write "

& "GIF" width = "18" height = "25"> "

NEXT

%>

Emergency friends want to ask, your counter value shows 6-digit count, if you want to display 8 digits, what should I do? Don't worry, wait for me to finish the next example, I will give a form. This counter has a disadvantage that each refresh page counter adds 1, because each page is refreshed, the system considers you to reclaim the page; and if you don't enter the website from the home page, the counter does not change the count. If you want

More precise, just give the above code, put it in your global.asa's session_onstart so that only the new user enters the website, the counter will add 1. The user who has already entered the website refreshed, and does not cause a change in the counter count, and no matter which page you enter, the counter can capture you.