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

Source: Internet
Author: User
Tags servervariables

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

I made a website, www.zydn.net, of course. Like everyone else, I also set up a counter on it. Every day I watched the counter constantly turning over. I was very happy, but then I thought again, who saw my website? Are new friends or old friends? I don't know how many times they came. I still want to assign a number to all of them, so I applied for free statistics, but I was not satisfied with changing N in a row. Alas, it seems that, only by yourself can you get enough food and clothing. If you do what you want, you will be able to do it. You will also find a high-handed joke.

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 "determines whether 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' if it is a new visitor, add a record to the database.
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' 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, write it down.
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 'set the cookie expiration time, so that I will not recognize it when one year arrives.

End if

Rs. Update' the records are all written down. update the database.
Rs. close '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 should be done well. There are more than 20 lines of code and a very simple small program. But I think there must be some tips for experts. Some talented friends will not forget to teach AI,

After the program is written, how can I put it on the page? Just find a place on the homepage and add the following code: .

The next step is to display the record. If there are more people who can do better than alicloud, it is still ugly.
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 displayed pages **************** 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
%>

<Head>
<Title> excellent computer Statistics </title>
</Head>

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

There are a total of <% Response. Write zs %> records, which are displayed on each page of the <% Response. Write page3 %> page: [<a href = "dispcont. asp? Pag = <% = page3 %> & pa = 15 "> 15], [<a href =" dispcont. asp? Pag = <% = page3 %> & pa = 20 "> 20], [<a href =" dispcont. asp? Pag = <% = page3 %> & pa = 30 "> 30], [<a href =" dispcont. asp? Pag = <% = page3 %> & pa = 40 "> 40]
[<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 of pages per page * Number of pages per page
If sn> zs then sn = 0
Rs. move sn, 1
*
%> </Tr> </table>
</Div> <table style = "font-size: 9pt "width =" 100% "bordercolorlight =" #000000 "border =" 1 "bordercolordark =" # FFFFFF "bgcolor =" # A4D1E8 "cellspacing =" 0 "cellpadding =" 3">
<Tr> <td> NO. </td> <td> last visit homepage </td> <td> last visit IP </td> <td> homepage Times </td> <td> first access 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
Next
Rs. close
%>
<Tr> <td> total visits <% = zs %> </td> <% = cs100 %> </td> <td> The number of visits is 1: <% = cs1 %> </td> <td> total access times <% = csi %> </td> <td> today's access volume: <% = csdat1 %> </td> </tr>
</Table>

* Above is the complete page display.. No record is considered.

You Can slightly change, add more functions, well, finally please pay attention to a site: http://www.zydn.net
Here is a demo of this applet,

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.