ASP instance: Access for the background database of the Web site statistics System

Source: Internet
Author: User
Tags date current time empty ip number connect servervariables table name
access| Background | data | database | statistics

A statistical example of how visitors can number, record access times, IP, and time.

I use the Access library as an example, in fact, with SQL Server Library also just change the statement of the link library, the library structure is as follows

Library filename: CONT. ASP was originally Cont.mdb but after construction, the extension was changed to ASP, in case the library was downloaded.
Table Name: Tab


Field Name Data type description

ID AutoNumber number of Visitors
IP text is used to record the IP of a visitor
DAT1 date time to record the last visit of the visitor
DAT date time is used to record the time of the first visit of a visitor

CS number, Integer used to record the number of visitors visited

The program is simple, only two files, dispcont.asp to display statistical results, contpage.asp for statistical information,

First Look at the contpage.asp is the first statistics, the code is as follows:

<%
Set conn=server.createobject ("ADODB. Connection ")
Connstr= "dbq=" +server.mappath ("cont.asp") + ";D efaultdir=;D river={microsoft Access DRIVER (*.mdb)};"
Conn.Open ConnStr ' * * The above statement is used to connect the library, cont.asp is the library file name.
Keren=request.cookies ("Keren") ' reads Cookies,cookies's name: "Keren", haha. Ah Yu's e-literature stinks, only know how to use pinyin.
If keren= "" then "Judge Cookees is not empty, if it is empty, then must be new friends, 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's a new visitor, add a record in the library.
RS ("cs") =1 ' note the number of visits is 1
RS ("IP") =request.servervariables ("remote_addr") ' Notes IP,
RS ("Dat") =now ' notes the current date time,
RS ("DAT1") =date ' make a note of the current date, later used for the first visit,
Response.Cookies ("Keren") =rs ("id") "writes a cookie, and the content is the same as the ID."
Response.Cookies ("Keren"). expires=date+365 ' Set the effective date for cookies from now on, 365 days,
Else ' is the way to deal with new friends, what about old friends? Look at the following:
Sql= "SELECT * from tab where id=" &keren "to the library to find out the records of our old friends.
Set Rs=server.createobject ("ADODB. Recordset ")
Rs. Open Sql,conn, 1, 3
RS ("CS") =rs ("cs") +1 ' OK, find it, add the number of visits 1
RS ("IP") =request.servervariables ("remote_addr") ' Look at his IP number, write it down.
RS ("Dat") =now ' down the current time, the time of the last visit,
Response.Cookies ("Keren") =rs ("id") "To write cookies again, I do not know if this is superfluous, did not try."
Response.Cookies ("Keren"). expires=date+365 ' Set cookies expiration time, lest I should not recognize him in a year.
End If
Rs.update ' should write down, update the library.
Rs.close ' closes the Recordset object.
Set conn=nothing ' Release Conn, I still think connection to follow with Guancai to, put to the session I think the most undesirable. 4
%>

Well, the record is done, there are more than 20 lines of code, very simple a small program, the program has been written, how to put it on the page? Very simple, on the home page to find a place, plus this line of code: on the line.

The next step is to show the records.

file name: dispcont.asp, see Code:

<%
Set conn=server.createobject ("ADODB. Connection ")
Connstr= "dbq=" +server.mappath ("cont.asp") + ";D efaultdir=;D river={microsoft Access DRIVER (*.mdb)};"
Conn.Open ConnStr ' * * The above statement is used to connect the library, cont.asp is the library file name.

Page3=request ("Pag")
If page3= "" "Then Page3=session (" Contpag ") ' page number, current paging
If page3= "" Then page3= "1"

Pa=request ("Pa")
If Pa= "" Then Pa=session ("Contpa") ' Show number per page
If Pa= "" then pa=15 "default per page display 15, can be arbitrarily changed
Session ("Contpag") =page3
Session ("CONTPA") =pa
Pages=pa ' Display quantity per page *************** above a program to implement paging function

Sql= "SELECT * from tab order By-dat,-id"
Dim rs
Set rs=server.createobject ("ADODB. RecordSet ")
Rs. Open sql,conn,1,1
Csi=0
Cs1=0
Cs100=0
Csdat1=0
Do as 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
' *****************************************************8 the following procedure for paging display
%>

<title> Web Teaching Online statistics </title>

<body style= "font-size:9pt" bgcolor= "#D8EDF8" >

A total of <%response.write zs%> Records, now <%response.write page3%> page per page display: [<a href=] dispcont.asp?pag=<%= Page3%>&pa=15 ">15], [<a href=" dispcont.asp?pag=<%=page3%>&pa=20 ">20] strip, [<a href=" Dispcont.asp?pag=<%=page3%>&pa=30 ">30] strip, [<a href=" dispcont.asp?pag=<%=page3%>&pa=40 " &GT;40] Article
[<a href= "dispcont.asp" > Refresh]
<div align= "Left" >
<table border= "0" cellpadding= "0" style= "font-size:9pt" >
<tr><td> page </td><%page2=1
For I=1 to ZS step pages
If Page3=cstr (Page2) Then
%&GT;&LT;TD >[<%response.write page2%>]</td>
<% Else%>
&LT;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 of pages per page * Number of pages-per-page display
If Sn>zs then sn=0
Rs.move sn,1
"********************************** above paragraph 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 visit home </td><td> last visit ip</td><td> home number </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> number of visits 100 times <%=cs100%> </td><td> Total number of visits to 1:<%=cs1%></td><td> <%=csi%></td><td> visits today:<%=csdat1%> </td></tr>
</table>

' **************************** above is the complete pagination display all copy down is available. There is no consideration of a record.



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.