A statistical production example that can number visitors and record the number of visits, IP, and time
I made a website, www.zydn.net. Of course, like everyone else, I also put a counter on it. I saw that the counter kept turning over every day, and I felt very happy. But then I thought, who read it? My website? Are they new friends or old friends? I don’t know how many times they came, and I wanted to number them all, so I applied for free statistics, but I tried N numbers in a row and was still not satisfied. Alas, it seems that the only way to have enough food and clothing is to do it myself. Just do it. It's dry, the level is stinky, I hope the experts will laugh at it, and I won't ask for advice.
I took the ACCESS library as an example. In fact, to use the SQL SERVER library, you only need to change the statement of the link library.
The library structure is as follows
Library file name: CONT.ASP It was originally CONT.MDB but after it was built, the extension was changed to ASP to prevent the library from being downloaded.
Table name: tab
Field name data type description
ID Automatically number the visitor's number
IP text is used to record the visitor’s IP
dat1 date and time is used to record the last time the visitor visited
dat date and time is used to record the time of the visitor's first visit
CS number, integer used to record the number of visitor visits
The program is very simple, with only two files, dispcont.asp is used to display statistical results, and contpage.asp is used for statistical information.
Let’s first look at the statistics of CONTPAGE.ASP. The code is as follows:
<%
Set Conn=Server.CreateObject(ADODB.Connection)
Connstr=DBQ=+server.mappath(cont.asp)+;DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};
Conn.Open connstr '*****The above statement is used to connect to the library, cont.asp is the library file name.
keren=request.cookies(keren) 'Read cookies, the name of cookies is: keren, haha. . Ah Yu's E is a literary stinker, and he only knows how to use pinyin.
if keren= then 'Determine whether cookes is empty. If it is empty, then it must be a new friend, otherwise it is an old friend.
sql=SELECT * FROM tab where id=-1
set rs=server.createobject(ADODB.Recordset)
rs.Open sql,conn, 1, 3
rs.addnew 'If it is a new visitor, add a new record in the library.
rs(cs)=1 'Remember the number of visits as 1
rs(ip)=request.servervariables(remote_addr) 'Remember the IP,
rs(dat)=now 'Remember the current date and time,
rs(dat1)=date 'Remember the current date and use it as the date of the first visit later.
response.cookies(keren)=rs(id) 'Write a cookie, the content is the same as the ID.
response.cookies(keren).expires=date+365 'Set the validity date of cookies starting from now, 365 days,
else 'The above is how to deal with new friends. What should we do with old friends? Watch below:
sql=SELECT * FROM tab where id=&keren 'Go to the database to find the records of our old friends
set rs=server.createobject(ADODB.Recordset)
rs.Open sql,conn, 1, 3
rs(cs)=rs(cs)+1 'Okay, found it, add 1 to the number of visits
rs(ip)=request.servervariables(remote_addr) 'Look at his IP and write it down.
rs(dat)=now 'Remember the current time, which is the time of the last visit,
response.cookies(keren)=rs(id) 'Write cookies in again. I don't know if this sentence is redundant, I haven't tried it.
response.cookies(keren).expires=date+365 'Set the cookie expiration time so that I won't recognize it when one year is up.
end if
rs.update 'I have written down everything that should be noted, let’s update the library.
rs.close 'Close the recordset object.
set conn=nothing 'Release conn, I still think that the connection should be opened and closed at any time. I think it is most undesirable to put it in SESSION. 4
%>
Okay, the record is ready. There are more than 20 lines of code. It is a very simple small program, but I think that experts must have clever tricks. Friends who have clever tricks don’t forget to teach Ayu.
Once the program is written, how do I put it on the page? It's very simple, just find a place on the homepage and add this line of code: <img src=contpage.asp width=0 height=0>.
The next step is to show the records. There are many people who can do better than Ayu, but I still want to show off my ugliness.
File name: dispcont.asp, please see the code:
<%
Set Conn=Server.CreateObject(ADODB.Connection)
Connstr=DBQ=+server.mappath(cont.asp)+;DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};
Conn.Open connstr '*****The above statement is used to connect to the library, cont.asp is the library file name.
page3=request(pag)
if page3= then page3=session(contpag) 'Number of pages, current page
if page3= then page3=1
pa=request(pa)
if pa= then pa=session(contpa) 'Display number per page
if pa= then pa=15 'By default, 15 items are displayed on each page, which can be changed arbitrarily.
session(contpag)=page3
session(contpa)=pa
pages=pa 'Display number per page******************The above program is used to implement the paging function
SQL=SELECT * FROM tab order by -dat,-id
dimrs
Set rs=Server.CreateObject(ADODB.RecordSet)
rs.Open sql,conn,1,1
csi=0
cs1=0
cs100=0
csdat1=0
do while not rs.eof
csi=csi+rs(cs)
if rs(cs)=1 then cs1=cs1+1
if rs(cs)>=100 then cs100+1
if datevalue(rs(dat))=date then
csdat1=csdat1+1
end if
rs.movenext
loop
ZS=RS.RECORDCOUNT
'************************************************ ****8The following program is used for paging display
%>
<head>
<title>Excellent Computer Statistics</title>
</head>
<body style=font-size: 9pt bgcolor=#D8EDF8>
There are a total of <%Response.Write zs%> records, and the current page is <%Response.Write page3%>. Each page displays: [<a href=dispcont.asp?pag=<%=page3%>&pa=15>15 ] items, [<a href=dispcont.asp?pag=<%=page3%>&pa=20>20] items, [<a href=dispcont.asp?pag=<%=page3%>&pa=30>30] items, [<a href=dispcont.asp?pag=<%=page3%>&pa=40>40] items
[<a href=dispcont.asp>Refresh]
<div align=left>
<table border=0 cellpadding=0 style=font-size: 9pt>
<tr><td>Page number</td><%page2=1
for i=1 to zs step pages
if page3=cstr(page2) then
%><td >[<%Response.Write page2%>]</td>
<% else %>
<td ><a href=dispcont.asp?pag=<%Response.Write page2%>>[<%Response.Write page2%>]</td>
<% end if
page2=page2+1
next
sn=pages*(page3-1) 'Current record number=number displayed per page*number of pages-number displayed per page
if sn>zs then sn=0
rs.move sn,1
'************************************The above paragraph is used for paging
%> </tr></table>
</div> <table style=font-size: 9pt width=100% bordercolorlight=#000000 border=1 bordercolordark=#FFFFFF bgcolor=#A4D1E8 cellspacing=0 cellpadding=3>
<tr><td>Number</td><td>Last visited homepage</td><td>Last visited IP</td><td>Number of homepages</td><td>First visit date</td> </tr><%
for i=1 to pages
Response.Write </tr>
Response.Write <td>&rs(ID)&</td>
Response.Write <td>&rs(dat)&</td>
Response.Write <td>&rs(IP)&</td>
Response.Write <td>&rs(CS)&</td>
Response.Write <td>&rs(DAT1)& </td>
Response.Write </tr>
rs.movenext
if rs.eof then exit for
next
rs.close
%>
<tr><td>Total<%=zs%></td><td>The number of visits is more than 100 times<%=cs100%> </td><td>The number of visits is 1:<% =cs1%></td><td>Total number of visits<%=csi%></td><td>Visits today: <%=csdat1%></td></tr>
</table>
'****************************** The above is the complete paginated display and can be used after copying it all. The case where there is not a single record is not considered.