Setting up Web page counters with ASP

Source: Internet
Author: User
Tags count readline root directory
There are usually two simple ways to set up a Web page counter with ASP, one is to set up Global.asa, and the other is to write an ASP file to count directly. Generally use a document to save the browsing quantity.

1, with Global.asa to write counters write a Global.asa file, stored in the root directory of the virtual directory, the source code is as follows:


<script language = "Vbscript" runat= "Server" >
Sub Application_OnStart ()
Countfile=server.mappath ("counter") + "/counter.txt"
Set Fso=server.createobject ("Scripting.FileSystemObject")
Set FILE=FSO. OpenTextFile ("Countfile")
\ ' Read access and assign to application variable number
Application ("number") =file.readline
File.close
End Sub
Sub Session_OnStart ()
If IsEmpty (Session ("hasbeenconnected")) Then
Application.Lock
Application ("number") =application ("number") +1
Applicaiotn.unlock
End If
Session ("hasbeenconnected") =true
End Sub
Sub Application_OnEnd ()
Countfile=server.mappath ("counter") + "/counter.txt"
Set Fso=server.createobject ("Scripting.FileSystemObject")
Set FILE=FSO. CreateTextFile ("Countfile", True)
\ ' writes the current value using the WriteLine method
File.writeline (Application ("number"))
File.close
End Sub
</script>
Write <%response in the Web page when the counter is invoked. Write ("You are the first" &number& "Visitor!") %>, but the calling page must also be an ASP page. This method has disadvantages, is a lot of personal home page space does not support running Global.asa, that is, not a user-built virtual directory, so it does not run correctly.

2, directly write a counter.asp to count my own now use the counter is this, and can be called on any page, call the method is:

<scriptsrc= "http://xxx.xxx.xxx/counter.asp?id=abc&num=6&style=1" ></script >
Where ID=ABC indicates that the user name is ABC, the Counter/abc.txt store count value should be established;
Num=6 is the number of display digits of the counter;
Style=1 for counter style, the establishment of counter/style1/0~9.gif can be added to a variety of styles.

The source code is as follows:

<%
Set Fso=server. CreateObject ("Scripting.FileSystemObject")
Filepath=server.mappath ("counter") + "/" +request ("id") + ". txt"
Set Temp=fso.opentextfile (filepath,1)
Count=temp.readline
Temp.close
If IsEmpty (Session ("Connected")) then
Set Temp=fso.opentextfile (filepath,2)
Application.Lock
Count=count+1
Temp.writeline (count)
Application.UnLock
Temp.close
End If
Set temp=nothing
Set fso=nothing
Session ("Connected") =true
Numlength=len (count)
If request ("num") = "" Then
Maxnum=6
Else
Maxnum=cint (Request ("num")
End If
If request ("style") = "" Then
style= "1"
Else
Style=request ("style")
End If
For I=1 to Maxnum step 1
If I<=maxnum-numlength Then
Countimage= "</img>"
Response.Write "document.write (\ ' &countimage&"); "
Else
Countimage= "</img>"
Response.Write "document.write (\ ' &countimage&"); "
End If
Next
%>



Related Article

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.