Website Statistics System with Access as the background database

Source: Internet
Author: User
Tags servervariables
An example of how to create a statistical instance that can number visitors, record ACCESS times, IP addresses, and time. I use the ACCESS database as an example, in fact, you only need to change the statement of the Link Library in the SQLSERVER database. The library structure is as follows: database file name: CONT. ASP is CONT. when MDB is created, the extension is changed to ASP to prevent the database from being downloaded. Table Name: Number of tab Field Names

An example of how to create a statistical instance that can number visitors, record ACCESS times, IP addresses, and time. I use the ACCESS database as an example, in fact, to use the SQL SERVER database, you only need to change the statement of the Linked database. The library structure is as follows: database file name: CONT. ASP is CONT. when MDB is created, the extension is changed to ASP to prevent the database from being downloaded. Table Name: Number of tab Field Names

A statistical production instance that can number visitors, record access times, IP addresses, and time

I use the ACCESS database as an example. In fact, you only need to modify the statements of the link database in the SQL SERVER database. The library structure is as follows:

Library File Name: CONT. ASP is originally CONT. MDB, but after being created, change the extension to ASP to prevent the library from being downloaded.
Table Name: tab


Field name data type description

ID: the visitor ID.
IP text is used to record the visitor's IP address
Dat1 date and time is used to record the last access time of the visitor
Dat date and time is used to record the first access time of a visitor

CS number, integer used to record visitor visits

The program is very simple. There are only two files. dispcont. asp is used to display the statistical results, and contpage. asp is used to collect statistics,

Let's take a look at the CONTPAGE. ASP statistics. The Code is as follows:

<%
Set Conn = Server. CreateObject ("ADODB. Connection ")
Connstr = "DBQ =" + server. mappath ("cont. asp") + "; DefaultDir =; DRIVER = {Microsoft Access Driver (*. mdb )};"
The preceding statement Conn. Open connstr ''' ***** is used to connect to the database. cont. asp is the file name of the database.
Keren = request. cookies ("keren") ''. The cookie name is" keren ". Haha .. AI's E-literature stinks and only understands pinyin.
If keren = "" then'', judge whether the cookees is empty. if it is empty, 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'' adds a record to the database if it is a new visitor.
Rs ("cs") = 1' write down the number of visits to 1
Rs ("ip") = request. servervariables ("remote_addr") ''write down the IP address,
Rs ("dat") = now ''write down the current date and time,
Rs ("dat1") = date' to write down the current date, which will be used for the first visit in the future,
Response. cookies ("keren") = rs ("id") ''write a cookie with the same content as the ID.
Response. cookies ("keren"). expires = date + 365 ''set the cookie validity period from now on, 365 days,
Else ''and above are solutions for new friends. What should we do for old friends? See the following:
SQL = "SELECT * FROM tab where id =" & keren'' go to the database to find records of our old friends.
Set rs = server. createobject ("ADODB. Recordset ")
Rs. Open SQL, conn, 1, 3
Rs ("cs") = rs ("cs") + 1' okay, find it, add 1 to the number of visits
Rs ("ip") = request. servervariables ("remote_addr") ''to check the IP address.
Rs ("dat") = now ''write down the current time, that is, the last access time,
Response. cookies ("keren") = rs ("id") ''then write the cookies. I don't know if this sentence is redundant and I didn't try it.
Response. cookies ("keren"). expires = date + 365 ''sets the cookie expiration time, so that I will not recognize it when one year arrives.
End if
Rs. Update' indicates all records. update the database.
Rs. close ''to close the recordset object.
Set conn = nothing ''to release the conn. I still think that the connection must be enabled with the function. In the SESSION, I think it is the least desirable. 4
%>

Well, the record is ready. There are more than 20 lines of code. It's a very simple small program. The program has been written. How can we put it on the page? It's easy. Just find a place on the homepage and add this line of code.

The next step is to display the record.

File Name: dispcont. asp. Please refer to the Code:

<%
Set Conn = Server. CreateObject ("ADODB. Connection ")
Connstr = "DBQ =" + server. mappath ("cont. asp") + "; DefaultDir =; DRIVER = {Microsoft Access Driver (*. mdb )};"
The preceding statement Conn. Open connstr ''' ***** is used to connect to the database. cont. asp is the file name of the database.

Page3 = request ("pag ")
If page3 = "" then page3 = session ("contpag") ''page, current page
If page3 = "" then page3 = "1"

Pa = request ("pa ")
If pa = "" then pa = session ("contpa") ''displayed per page
If pa = "" then pa = 15'' by default, 15 entries are displayed on each page.
Session ("contpag") = page3
Session ("contpa") = pa
Pages = pa'' the number of pages displayed on each page *************** the preceding program is used to implement the 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 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
''************************************** * *************** The following program is used for paging display
%>


Online statistics of web teaching network

Total <% Response. write zs %> record, which is <% Response. the Write page3 %> page displays: [& pa = 15 "> 15], [& pa = 20"> 20], [& pa = 30 "> 30], and [& pa = 40"> 40]
[Refresh]
















<% Page2 = 1For I = 1 to zs step pagesIf page3 = cstr (page2) then%> <% Else %> <% End ifPage2 = page2 + 1NextSn = pages * (page3-1) ''current record number = number of pages per page * Number of pages per pageIf sn> zs then sn = 0Rs. move sn, 1''*********************************** The above section is used for paging%>
Page number[<% Response. Write page2 %>]"> [<% Response. Write page2 %>]


















<%For I = 1 to pagesResponse. Write""Response. Write" "Response. Write" "Response. Write" "Response. Write" " Response. Write" "Response. Write" "Rs. movenextIf rs. eof then exitNextRs. close%>
No. Last visit Homepage Last accessed IP Address Homepage times First access date
"& Rs (" ID ")&""& Rs (" dat ")&""& Rs (" IP ")&""& Rs (" CS ")&""& Rs (" DAT1 ")&"
Total <% = zs %> <% = Cs100 %> Access times: <% = cs1 %> Total access times <% = csi %> Traffic Volume today: <% = csdat1 %>

''****************************** The above is the complete page display, all copied available. No record is considered.

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.